Skip to content

Commit

Permalink
Merge pull request #315 from mohssenfathi/logout
Browse files Browse the repository at this point in the history
Logout API
  • Loading branch information
mohssenfathi authored Sep 12, 2024
2 parents 93a88d8 + a371aaf commit 6dec2b6
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 91 deletions.
4 changes: 4 additions & 0 deletions Sources/UberAuth/AuthProviding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public protocol AuthProviding {
prefill: Prefill?,
completion: @escaping (Result<Client, UberAuthError>) -> ())

func logout() -> Bool

func handle(response url: URL) -> Bool

var isLoggedIn: Bool { get }
}

extension AuthProviding where Self == AuthorizationCodeAuthProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ public final class AuthorizationCodeAuthProvider: AuthProviding {
self.completion = authCompletion
}

public func logout() -> Bool {
tokenManager.deleteToken(identifier: TokenManager.defaultAccessTokenIdentifier)
}

public func handle(response url: URL) -> Bool {
guard responseParser.isValidResponse(url: url, matching: redirectURI) else {
return false
Expand All @@ -168,6 +172,10 @@ public final class AuthorizationCodeAuthProvider: AuthProviding {
return true
}

public var isLoggedIn: Bool {
tokenManager.getToken(identifier: TokenManager.defaultAccessTokenIdentifier) != nil
}

// MARK: - Private

private func executeLogin(authDestination: AuthDestination,
Expand Down
41 changes: 40 additions & 1 deletion Sources/UberAuth/UberAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ protocol AuthManaging {

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

func logout()

func handle(_ url: URL) -> Bool

var isLoggedIn: Bool { get }
}

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

/// Clears any saved auth information from the keychain
/// If `currentAuthContext` exists, logs out using the stored auth context
/// Otherwise, attempts to delete the saved auth token directly using the internal TokenManager
public static func logout() {
auth.logout()
}

/// Attempts to extract auth information from the provided URL.
/// This method should be called from the implemeting application's openURL function.
///
Expand All @@ -63,9 +74,22 @@ public final class UberAuth: AuthManaging {
auth.handle(url)
}

/// A computed property that indicates if auth information is saved in the keychain
/// First checks for saved token information using the current auth provider
/// If no auth provider exists, falls back to the default token identifier
public static var isLoggedIn: Bool {
auth.isLoggedIn
}

// MARK: Internal
// MARK: AuthManaging

init(currentContext: AuthContext? = nil,
tokenManager: TokenManaging = TokenManager()) {
self.currentContext = currentContext
self.tokenManager = tokenManager
}

func login(context: AuthContext = .init(),
completion: @escaping AuthCompletion) {
context.authProvider.execute(
Expand All @@ -76,15 +100,30 @@ public final class UberAuth: AuthManaging {
currentContext = context
}

func logout() {
guard let currentContext else {
tokenManager.deleteToken(identifier: TokenManager.defaultAccessTokenIdentifier)
return
}
currentContext.authProvider.logout()
self.currentContext = nil
}

func handle(_ url: URL) -> Bool {
guard let currentContext else {
return false
}
return currentContext.authProvider.handle(response: url)
}

var isLoggedIn: Bool {
currentContext?.authProvider.isLoggedIn ?? (tokenManager.getToken(identifier: TokenManager.defaultAccessTokenIdentifier) != nil)
}

private static let auth = UberAuth()

private var currentContext: AuthContext?
var currentContext: AuthContext?

var tokenManager: TokenManaging = TokenManager()
}

22 changes: 16 additions & 6 deletions examples/UberSDK/UberSDK/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,19 @@ final class Content {
var isPrefillExpanded: Bool = false
var response: AuthReponse?
var prefillBuilder = PrefillBuilder()
var isLoggedIn: Bool {
UberAuth.isLoggedIn
}

func login() {

var promt: Prompt = []
if shouldForceLogin { promt.insert(.login) }
if shouldForceConsent { promt.insert(.consent) }
var prompt: Prompt = []
if shouldForceLogin { prompt.insert(.login) }
if shouldForceConsent { prompt.insert(.consent) }

let authProvider: AuthProviding = .authorizationCode(
shouldExchangeAuthCode: isTokenExchangeEnabled,
prompt: promt
prompt: prompt
)

let authDestination: AuthDestination = {
Expand Down Expand Up @@ -94,6 +97,11 @@ final class Content {
)
}

func logout() {
UberAuth.logout()
response = nil
}

func openUrl(_ url: URL) {
UberAuth.handle(url)
}
Expand Down Expand Up @@ -219,9 +227,11 @@ struct ContentView: View {
}

Button(
action: { content.login() },
action: {
content.isLoggedIn ? content.logout() : content.login()
},
label: {
Text("Login")
Text(content.isLoggedIn ? "Logout" : "Login")
.frame(maxWidth: .infinity, alignment: .center)
}
)
Expand Down
Loading

0 comments on commit 6dec2b6

Please sign in to comment.