-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Token Storage + UI #298
Merged
Merged
Token Storage + UI #298
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
// | ||
// Copyright © Uber Technologies, Inc. All rights reserved. | ||
// | ||
|
||
|
||
import Foundation | ||
import UberCore | ||
import UIKit | ||
|
||
|
||
/// | ||
/// A protocol to respond to Uber LoginButton events | ||
/// | ||
public protocol LoginButtonDelegate: AnyObject { | ||
|
||
/// | ||
/// The login button attempted to log out | ||
/// | ||
/// - Parameters: | ||
/// - button: The LoginButton instance that attempted logout | ||
/// - success: A bollean indicating whether or not the logout was successful | ||
func loginButton(_ button: LoginButton, | ||
didLogoutWithSuccess success: Bool) | ||
|
||
|
||
/// | ||
/// The login button completed authentication | ||
/// | ||
/// - Parameters: | ||
/// - button: The LoginButton instance that completed authentication | ||
/// - result: A Result containing the authentication response. If successful, contains the Client object returned from the UberAuth authenticate function. If failed, contains an UberAuth error indicating the failure reason. | ||
func loginButton(_ button: LoginButton, | ||
didCompleteLoginWithResult result: Result<Client, UberAuthError>) | ||
} | ||
|
||
/// | ||
/// A protocol to provide content for the Uber LoginButton | ||
/// | ||
public protocol LoginButtonDataSource: AnyObject { | ||
|
||
/// | ||
/// Provides an optional AuthContext to be used during authentication | ||
/// | ||
/// - Parameter button: The LoginButton instance requesting the information | ||
/// - Returns: An optional AuthContext instance | ||
func authContext(_ button: LoginButton) -> AuthContext | ||
} | ||
|
||
public final class LoginButton: UberButton { | ||
|
||
// MARK: Public Properties | ||
|
||
/// The LoginButtonDelegate for this button | ||
public weak var delegate: LoginButtonDelegate? | ||
|
||
public weak var dataSource: LoginButtonDataSource? | ||
|
||
// MARK: Private Properties | ||
|
||
private var buttonState: State { | ||
tokenManager.getToken( | ||
identifier: Constants.tokenIdentifier | ||
) != nil ? .loggedIn : .loggedOut | ||
} | ||
|
||
private let tokenManager: TokenManaging | ||
|
||
// MARK: Initializers | ||
|
||
public override init(frame: CGRect) { | ||
self.tokenManager = TokenManager() | ||
super.init(frame: frame) | ||
configure() | ||
} | ||
|
||
public required init?(coder: NSCoder) { | ||
self.tokenManager = TokenManager() | ||
super.init(coder: coder) | ||
configure() | ||
} | ||
|
||
public init(tokenManager: TokenManaging = TokenManager()) { | ||
self.tokenManager = tokenManager | ||
super.init(frame: .zero) | ||
configure() | ||
} | ||
|
||
// MARK: UberButton | ||
|
||
override public var title: String { | ||
buttonState.title | ||
} | ||
|
||
override public var image: UIImage? { | ||
UIImage( | ||
named: "uber_logo_white", | ||
in: .module, | ||
compatibleWith: nil | ||
)?.withRenderingMode(.alwaysTemplate) | ||
} | ||
|
||
// MARK: Private | ||
|
||
private func configure() { | ||
addTarget(self, action: #selector(buttonTapped), for: .touchUpInside) | ||
} | ||
|
||
@objc private func buttonTapped(_ sender: UIButton) { | ||
switch buttonState { | ||
case .loggedIn: | ||
logout() | ||
case .loggedOut: | ||
login() | ||
} | ||
} | ||
|
||
private func login() { | ||
let defaultContext = AuthContext( | ||
authProvider: .authorizationCode( | ||
shouldExchangeAuthCode: true | ||
) | ||
) | ||
let context = dataSource?.authContext(self) ?? defaultContext | ||
UberAuth.login(context: context) { [weak self] result in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: weak self likely not needed, since UberAuth is being referred statically There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. Will fix in the next PR. |
||
guard let self else { return } | ||
delegate?.loginButton(self, didCompleteLoginWithResult: result) | ||
update() | ||
} | ||
} | ||
|
||
private func logout() { | ||
// TODO: Implement UberAuth.logout() | ||
tokenManager.deleteToken(identifier: Constants.tokenIdentifier) | ||
update() | ||
} | ||
|
||
// MARK: State | ||
|
||
enum State { | ||
case loggedIn | ||
case loggedOut | ||
|
||
var title: String { | ||
switch self { | ||
case .loggedIn: | ||
return NSLocalizedString( | ||
"Sign Out", | ||
bundle: .module, | ||
comment: "Login Button Sign Out Description" | ||
) | ||
.uppercased() | ||
mohssenfathi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case .loggedOut: | ||
return NSLocalizedString( | ||
"Sign In", | ||
bundle: .module, | ||
comment: "Login Button Sign In Description" | ||
) | ||
.uppercased() | ||
} | ||
} | ||
} | ||
|
||
// MARK: Constants | ||
|
||
private enum Constants { | ||
static let tokenIdentifier: String = TokenManager.defaultAccessTokenIdentifier | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
Sources/UberAuth/Resources/Media.xcassets/uber_logo_white.imageset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"filename" : "uber_logotype_white@1x.png", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"filename" : "uber_logotype_white@2x.png", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"filename" : "uber_logotype_white@3x.png", | ||
"scale" : "3x" | ||
} | ||
], | ||
"info" : { | ||
"version" : 1, | ||
"author" : "xcode" | ||
} | ||
} |
Binary file added
BIN
+1.02 KB
...th/Resources/Media.xcassets/uber_logo_white.imageset/uber_logotype_white@1x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.85 KB
...th/Resources/Media.xcassets/uber_logo_white.imageset/uber_logotype_white@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.75 KB
...th/Resources/Media.xcassets/uber_logo_white.imageset/uber_logotype_white@3x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: could be moved to a constant at class level.