Skip to content
This repository was archived by the owner on Jan 2, 2025. It is now read-only.

Commit 3374aa1

Browse files
committed
- Fixed setting default provider key for migration
1 parent a6c8455 commit 3374aa1

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

Sources/AuthenticationManager/AuthenticationProvider.swift

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,35 @@ extension AuthenticationManager {
1717
*/
1818
static var currentProvider: AuthenticationProvider? {
1919
get {
20-
guard let value = UserDefaults.standard.string(forKey: UserDefaultsKeys.authenticationProvider.rawValue) else { return nil }
20+
guard let value = UserDefaults.standard.string(forKey: UserDefaultsKeys.authenticationProvider.rawValue) else {
21+
self.setDefaultProviderKey()
22+
return self.currentProvider
23+
}
2124
return AuthenticationProvider(rawValue: value)
2225
}
23-
set { UserDefaults.standard.set(newValue?.rawValue, forKey: UserDefaultsKeys.authenticationProvider.rawValue) }
26+
set {
27+
let value = newValue?.rawValue ?? ""
28+
UserDefaults.standard.set(value, forKey: UserDefaultsKeys.authenticationProvider.rawValue)
29+
}
30+
}
31+
32+
33+
/**
34+
If a user is already signed in but the provider key is missing (e.g. because of earlier version), we need to deduct the provider by checking the login methods
35+
*/
36+
private static func setDefaultProviderKey() {
37+
if hasUser {
38+
if userIsAuthenticated {
39+
if authorizationKey != nil {
40+
currentProvider = .signInWithApple
41+
} else {
42+
currentProvider = .emailPassword
43+
}
44+
} else {
45+
currentProvider = .anonymous
46+
}
47+
} else {
48+
currentProvider = nil
49+
}
2450
}
2551
}

0 commit comments

Comments
 (0)