Skip to content

Commit 6dec2b6

Browse files
authored
Merge pull request #315 from mohssenfathi/logout
Logout API
2 parents 93a88d8 + a371aaf commit 6dec2b6

File tree

6 files changed

+245
-91
lines changed

6 files changed

+245
-91
lines changed

Sources/UberAuth/AuthProviding.swift

+4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ public protocol AuthProviding {
3333
prefill: Prefill?,
3434
completion: @escaping (Result<Client, UberAuthError>) -> ())
3535

36+
func logout() -> Bool
37+
3638
func handle(response url: URL) -> Bool
39+
40+
var isLoggedIn: Bool { get }
3741
}
3842

3943
extension AuthProviding where Self == AuthorizationCodeAuthProvider {

Sources/UberAuth/Authorize/AuthorizationCodeAuthProvider.swift

+8
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ public final class AuthorizationCodeAuthProvider: AuthProviding {
157157
self.completion = authCompletion
158158
}
159159

160+
public func logout() -> Bool {
161+
tokenManager.deleteToken(identifier: TokenManager.defaultAccessTokenIdentifier)
162+
}
163+
160164
public func handle(response url: URL) -> Bool {
161165
guard responseParser.isValidResponse(url: url, matching: redirectURI) else {
162166
return false
@@ -168,6 +172,10 @@ public final class AuthorizationCodeAuthProvider: AuthProviding {
168172
return true
169173
}
170174

175+
public var isLoggedIn: Bool {
176+
tokenManager.getToken(identifier: TokenManager.defaultAccessTokenIdentifier) != nil
177+
}
178+
171179
// MARK: - Private
172180

173181
private func executeLogin(authDestination: AuthDestination,

Sources/UberAuth/UberAuth.swift

+40-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ protocol AuthManaging {
3232

3333
func login(context: AuthContext, completion: @escaping AuthCompletion)
3434

35+
func logout()
36+
3537
func handle(_ url: URL) -> Bool
38+
39+
var isLoggedIn: Bool { get }
3640
}
3741

3842
/// Public interface for the uber-auth-ios library
@@ -53,6 +57,13 @@ public final class UberAuth: AuthManaging {
5357
)
5458
}
5559

60+
/// Clears any saved auth information from the keychain
61+
/// If `currentAuthContext` exists, logs out using the stored auth context
62+
/// Otherwise, attempts to delete the saved auth token directly using the internal TokenManager
63+
public static func logout() {
64+
auth.logout()
65+
}
66+
5667
/// Attempts to extract auth information from the provided URL.
5768
/// This method should be called from the implemeting application's openURL function.
5869
///
@@ -63,9 +74,22 @@ public final class UberAuth: AuthManaging {
6374
auth.handle(url)
6475
}
6576

77+
/// A computed property that indicates if auth information is saved in the keychain
78+
/// First checks for saved token information using the current auth provider
79+
/// If no auth provider exists, falls back to the default token identifier
80+
public static var isLoggedIn: Bool {
81+
auth.isLoggedIn
82+
}
83+
6684
// MARK: Internal
6785
// MARK: AuthManaging
6886

87+
init(currentContext: AuthContext? = nil,
88+
tokenManager: TokenManaging = TokenManager()) {
89+
self.currentContext = currentContext
90+
self.tokenManager = tokenManager
91+
}
92+
6993
func login(context: AuthContext = .init(),
7094
completion: @escaping AuthCompletion) {
7195
context.authProvider.execute(
@@ -76,15 +100,30 @@ public final class UberAuth: AuthManaging {
76100
currentContext = context
77101
}
78102

103+
func logout() {
104+
guard let currentContext else {
105+
tokenManager.deleteToken(identifier: TokenManager.defaultAccessTokenIdentifier)
106+
return
107+
}
108+
currentContext.authProvider.logout()
109+
self.currentContext = nil
110+
}
111+
79112
func handle(_ url: URL) -> Bool {
80113
guard let currentContext else {
81114
return false
82115
}
83116
return currentContext.authProvider.handle(response: url)
84117
}
85118

119+
var isLoggedIn: Bool {
120+
currentContext?.authProvider.isLoggedIn ?? (tokenManager.getToken(identifier: TokenManager.defaultAccessTokenIdentifier) != nil)
121+
}
122+
86123
private static let auth = UberAuth()
87124

88-
private var currentContext: AuthContext?
125+
var currentContext: AuthContext?
126+
127+
var tokenManager: TokenManaging = TokenManager()
89128
}
90129

examples/UberSDK/UberSDK/ContentView.swift

+16-6
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,19 @@ final class Content {
5454
var isPrefillExpanded: Bool = false
5555
var response: AuthReponse?
5656
var prefillBuilder = PrefillBuilder()
57+
var isLoggedIn: Bool {
58+
UberAuth.isLoggedIn
59+
}
5760

5861
func login() {
5962

60-
var promt: Prompt = []
61-
if shouldForceLogin { promt.insert(.login) }
62-
if shouldForceConsent { promt.insert(.consent) }
63+
var prompt: Prompt = []
64+
if shouldForceLogin { prompt.insert(.login) }
65+
if shouldForceConsent { prompt.insert(.consent) }
6366

6467
let authProvider: AuthProviding = .authorizationCode(
6568
shouldExchangeAuthCode: isTokenExchangeEnabled,
66-
prompt: promt
69+
prompt: prompt
6770
)
6871

6972
let authDestination: AuthDestination = {
@@ -94,6 +97,11 @@ final class Content {
9497
)
9598
}
9699

100+
func logout() {
101+
UberAuth.logout()
102+
response = nil
103+
}
104+
97105
func openUrl(_ url: URL) {
98106
UberAuth.handle(url)
99107
}
@@ -219,9 +227,11 @@ struct ContentView: View {
219227
}
220228

221229
Button(
222-
action: { content.login() },
230+
action: {
231+
content.isLoggedIn ? content.logout() : content.login()
232+
},
223233
label: {
224-
Text("Login")
234+
Text(content.isLoggedIn ? "Logout" : "Login")
225235
.frame(maxWidth: .infinity, alignment: .center)
226236
}
227237
)

0 commit comments

Comments
 (0)