Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
Add an optional reauth handler to BridgeClientAppManager
Browse files Browse the repository at this point in the history
  • Loading branch information
syoung-smallwisdom committed Mar 16, 2023
1 parent df03505 commit fac142b
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion SwiftPackage/Sources/BridgeClientUI/BridgeClientAppManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import JsonModel

fileprivate let kOnboardingStateKey = "isOnboardingFinished"

public protocol ReauthPasswordHandler {
func storedPassword(for session: UserSessionInfoObserver) -> String?
func clearStoredPassword()
}

/// This class is intended to be used as the `BridgeClient` app singleton. It manages login, app state,
/// app configuration, user configuration, notifications, and uploading files to Bridge services. It is intended
/// to be used in conjunction with a `UIApplicationDelegate` to handle start up and background
Expand Down Expand Up @@ -146,7 +151,7 @@ open class BridgeClientAppManager : UploadAppManager {
if bridgeAppStatus != .supported {
return .error
}
else if appConfig.isLaunching || userSessionInfo.loginState == .launching {
else if appConfig.isLaunching || userSessionInfo.loginState == .launching || reauthStatus == .pending {
return .launching
}
else if !userSessionInfo.isAuthenticated {
Expand All @@ -159,5 +164,34 @@ open class BridgeClientAppManager : UploadAppManager {
return .main
}
}

// MARK: Reauth handling

public var reauthPasswordHandler: ReauthPasswordHandler? = nil
var reauthStatus: ResourceStatus?

/// Override the default (which is to do nothing) and check to see if there is a
/// ``ReauthPasswordHandler`` set to get the password for this app.
open override func handleReauthFailed() -> Bool {
if let password = reauthPasswordHandler?.storedPassword(for: userSessionInfo) {
reauthStatus = .pending
reauthWithCredentials(password: password) { [weak self] status in
self?.reauthStatus = status
if status == .failed {
self?.reauthPasswordHandler?.clearStoredPassword()
self?.userSessionInfo.loginError = "Failed to reauth using stored credentials."
}
else if status == .retry {
// If the reauth failed, then we need to show the participant the login and have them
// attempt reauth or sign out.
self?.updateAppState()
}
}
return true
}
else {
return false
}
}
}

0 comments on commit fac142b

Please sign in to comment.