@@ -141,6 +141,10 @@ public final class AuthService {
141141 errorMessage = " "
142142 }
143143
144+ public var shouldHandleAnonymousUpgrade : Bool {
145+ currentUser? . isAnonymous == true && configuration. shouldAutoUpgradeAnonymousUsers
146+ }
147+
144148 public func signOut( ) async throws {
145149 do {
146150 try await auth. signOut ( )
@@ -167,15 +171,31 @@ public final class AuthService {
167171 }
168172 }
169173
174+ public func handleAutoUpgradeAnonymousUser( credentials credentials: AuthCredential ) async throws {
175+ do {
176+ try await currentUser? . link ( with: credentials)
177+ } catch let error as NSError {
178+ if error. code == AuthErrorCode . emailAlreadyInUse. rawValue {
179+ let context = AccountMergeConflictContext (
180+ credential: credentials,
181+ underlyingError: error,
182+ message: " Unable to merge accounts. Use the credential in the context to resolve the conflict. "
183+ )
184+ throw AuthServiceError . accountMergeConflict ( context: context)
185+ }
186+ throw error
187+ }
188+ }
189+
170190 public func signIn( credentials credentials: AuthCredential ) async throws {
171191 authenticationState = . authenticating
172192 do {
173- if currentUser ? . isAnonymous == true , configuration . shouldAutoUpgradeAnonymousUsers {
174- try await linkAccounts ( credentials: credentials)
193+ if shouldHandleAnonymousUpgrade {
194+ try await handleAutoUpgradeAnonymousUser ( credentials: credentials)
175195 } else {
176196 try await auth. signIn ( with: credentials)
177- updateAuthenticationState ( )
178197 }
198+ updateAuthenticationState ( )
179199 } catch {
180200 authenticationState = . unauthenticated
181201 errorMessage = string. localizedErrorMessage (
@@ -231,7 +251,13 @@ public extension AuthService {
231251 authenticationState = . authenticating
232252
233253 do {
234- try await auth. createUser ( withEmail: email, password: password)
254+ if shouldHandleAnonymousUpgrade {
255+ // TODO: - check this works. This is how it is done in previous implementation, but I wonder if this would fail
256+ let credential = EmailAuthProvider . credential ( withEmail: email, password: password)
257+ try await handleAutoUpgradeAnonymousUser ( credentials: credential)
258+ } else {
259+ try await auth. createUser ( withEmail: email, password: password)
260+ }
235261 updateAuthenticationState ( )
236262 } catch {
237263 authenticationState = . unauthenticated
@@ -278,6 +304,8 @@ public extension AuthService {
278304 throw AuthServiceError . invalidEmailLink
279305 }
280306 let link = url. absoluteString
307+ // TODO: - get anonymous id here and check against current user before linking accounts
308+ // put anonymous uid on link and get it back: https://github.com/firebase/FirebaseUI-iOS/blob/main/FirebaseEmailAuthUI/Sources/FUIEmailAuth.m#L822
281309 if auth. isSignIn ( withEmailLink: link) {
282310 let result = try await auth. signIn ( withEmail: email, link: link)
283311 updateAuthenticationState ( )
0 commit comments