@@ -124,7 +124,9 @@ public final class AuthService {
124124 guard let actionCodeSettings = configuration
125125 . emailLinkSignInActionCodeSettings else {
126126 throw AuthServiceError
127- . notConfiguredActionCodeSettings
127+ . notConfiguredActionCodeSettings (
128+ " ActionCodeSettings has not been configured for `AuthConfiguration.emailLinkSignInActionCodeSettings` "
129+ )
128130 }
129131 return actionCodeSettings
130132 }
@@ -285,7 +287,7 @@ public extension AuthService {
285287public extension AuthService {
286288 func sendEmailSignInLink( to email: String ) async throws {
287289 do {
288- let actionCodeSettings = try safeActionCodeSettings ( )
290+ let actionCodeSettings = try updateActionCodeSettings ( )
289291 try await auth. sendSignInLink (
290292 toEmail: email,
291293 actionCodeSettings: actionCodeSettings
@@ -304,10 +306,15 @@ public extension AuthService {
304306 throw AuthServiceError . invalidEmailLink
305307 }
306308 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
309+
309310 if auth. isSignIn ( withEmailLink: link) {
310- let result = try await auth. signIn ( withEmail: email, link: link)
311+ let anonymousUserID = CommonUtils . getQueryParamValue ( from: link, paramName: " ui_auid " )
312+ if shouldHandleAnonymousUpgrade, anonymousUserID == currentUser? . uid {
313+ let credential = EmailAuthProvider . credential ( withEmail: email, link: link)
314+ try await handleAutoUpgradeAnonymousUser ( credentials: credential)
315+ } else {
316+ let result = try await auth. signIn ( withEmail: email, link: link)
317+ }
311318 updateAuthenticationState ( )
312319 emailLink = nil
313320 }
@@ -318,6 +325,38 @@ public extension AuthService {
318325 throw error
319326 }
320327 }
328+
329+ private func updateActionCodeSettings( ) throws -> ActionCodeSettings {
330+ let actionCodeSettings = try safeActionCodeSettings ( )
331+ guard var urlComponents = URLComponents ( string: actionCodeSettings. url!. absoluteString) else {
332+ throw AuthServiceError
333+ . notConfiguredActionCodeSettings (
334+ " ActionCodeSettings.url has not been configured for `AuthConfiguration.emailLinkSignInActionCodeSettings` "
335+ )
336+ }
337+
338+ var queryItems : [ URLQueryItem ] = [ ]
339+
340+ if shouldHandleAnonymousUpgrade {
341+ if let currentUser = currentUser {
342+ let anonymousUID = currentUser. uid
343+ let auidItem = URLQueryItem ( name: " ui_auid " , value: anonymousUID)
344+ queryItems. append ( auidItem)
345+ }
346+ }
347+
348+ // We don't have config for forceSameDevice so it is set as default
349+ let forceSameDevice = " 1 "
350+ let sameDeviceItem = URLQueryItem ( name: " ui_sd " , value: forceSameDevice)
351+ queryItems. append ( sameDeviceItem)
352+
353+ urlComponents. queryItems = queryItems
354+ if let finalURL = urlComponents. url {
355+ actionCodeSettings. url = finalURL
356+ }
357+
358+ return actionCodeSettings
359+ }
321360}
322361
323362// MARK: - Google Sign In
0 commit comments