-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #298 from mohssenfathi/token-storage
Token Storage + UI
- Loading branch information
Showing
39 changed files
with
1,586 additions
and
665 deletions.
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 | ||
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() | ||
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" : "[email protected]", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"filename" : "[email protected]", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"filename" : "[email protected]", | ||
"scale" : "3x" | ||
} | ||
], | ||
"info" : { | ||
"version" : 1, | ||
"author" : "xcode" | ||
} | ||
} |
Binary file added
BIN
+1.02 KB
...th/Resources/Media.xcassets/uber_logo_white.imageset/[email protected]
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/[email protected]
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/[email protected]
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.