@@ -14,17 +14,6 @@ public protocol PhoneAuthProviderProtocol {
1414 @MainActor func verifyPhoneNumber( phoneNumber: String ) async throws -> String
1515}
1616
17- public enum AuthenticationProvider {
18- case email
19- case google
20- }
21-
22- public enum AuthenticationOperationType : String {
23- case signIn
24- case signUp
25- case deleteAccount
26- }
27-
2817public enum AuthenticationState {
2918 case unauthenticated
3019 case authenticating
@@ -153,17 +142,32 @@ public final class AuthService {
153142 updateAuthenticationState ( )
154143 }
155144
156- public func signIn ( with credentials: AuthCredential ) async throws {
145+ public func linkAccounts ( credentials credentials: AuthCredential ) async throws {
157146 authenticationState = . authenticating
158147 do {
159- try await auth . signIn ( with: credentials)
148+ try await currentUser ? . link ( with: credentials)
160149 updateAuthenticationState ( )
161150 } catch {
162151 authenticationState = . unauthenticated
163152 throw error
164153 }
165154 }
166155
156+ public func signIn( credentials credentials: AuthCredential ) async throws {
157+ authenticationState = . authenticating
158+ if currentUser? . isAnonymous == true , configuration. shouldAutoUpgradeAnonymousUsers {
159+ try await linkAccounts ( credentials: credentials)
160+ } else {
161+ do {
162+ try await auth. signIn ( with: credentials)
163+ updateAuthenticationState ( )
164+ } catch {
165+ authenticationState = . unauthenticated
166+ throw error
167+ }
168+ }
169+ }
170+
167171 func sendEmailVerification( ) async throws {
168172 if currentUser != nil {
169173 do {
@@ -181,11 +185,12 @@ public final class AuthService {
181185public extension AuthService {
182186 func signIn( withEmail email: String , password: String ) async throws {
183187 let credential = EmailAuthProvider . credential ( withEmail: email, password: password)
184- try await auth . signIn ( with : credential)
188+ try await signIn ( credentials : credential)
185189 }
186190
187191 func createUser( withEmail email: String , password: String ) async throws {
188192 authenticationState = . authenticating
193+
189194 do {
190195 try await auth. createUser ( withEmail: email, password: password)
191196 updateAuthenticationState ( )
@@ -252,7 +257,7 @@ public extension AuthService {
252257 }
253258 let credential = try await safeGoogleProvider. signInWithGoogle ( clientID: clientID)
254259
255- try await signIn ( with : credential)
260+ try await signIn ( credentials : credential)
256261 updateAuthenticationState ( )
257262 } catch {
258263 authenticationState = . unauthenticated
@@ -269,7 +274,7 @@ public extension AuthService {
269274 do {
270275 let credential = try await safeFacebookProvider
271276 . signInWithFacebook ( isLimitedLogin: limitedLogin)
272- try await signIn ( with : credential)
277+ try await signIn ( credentials : credential)
273278 updateAuthenticationState ( )
274279 } catch {
275280 authenticationState = . unauthenticated
@@ -290,7 +295,7 @@ public extension AuthService {
290295 do {
291296 let credential = PhoneAuthProvider . provider ( )
292297 . credential ( withVerificationID: verificationID, verificationCode: verificationCode)
293- try await signIn ( with : credential)
298+ try await signIn ( credentials : credential)
294299 updateAuthenticationState ( )
295300 } catch {
296301 authenticationState = . unauthenticated
0 commit comments