@@ -217,12 +217,20 @@ public final class AuthService {
217217
218218 try await user. link ( with: credentials)
219219 updateAuthenticationState ( )
220- } catch {
220+ } catch let error as NSError {
221+ authenticationState = . unauthenticated
222+
223+ // Check if reauthentication is needed
224+ if error. domain == AuthErrorDomain,
225+ error. code == AuthErrorCode . requiresRecentLogin. rawValue ||
226+ error. code == AuthErrorCode . userTokenExpired. rawValue {
227+ try await requireReauthentication ( )
228+ }
229+
221230 // Possible conflicts from user.link():
222231 // - credentialAlreadyInUse: credential is already linked to another account
223232 // - emailAlreadyInUse: email from credential is already used by another account
224233 // - accountExistsWithDifferentCredential: account exists with different sign-in method
225- authenticationState = . unauthenticated
226234 try handleErrorWithConflictCheck ( error: error, credential: credentials)
227235 }
228236 }
@@ -293,15 +301,37 @@ public extension AuthService {
293301 throw AuthServiceError . noCurrentUser
294302 }
295303
296- try await user. delete ( )
304+ do {
305+ try await user. delete ( )
306+ } catch let error as NSError {
307+ // Check if reauthentication is needed
308+ if error. domain == AuthErrorDomain,
309+ error. code == AuthErrorCode . requiresRecentLogin. rawValue ||
310+ error. code == AuthErrorCode . userTokenExpired. rawValue {
311+ try await requireReauthentication ( )
312+ } else {
313+ throw error
314+ }
315+ }
297316 }
298317
299318 func updatePassword( to password: String ) async throws {
300319 guard let user = auth. currentUser else {
301320 throw AuthServiceError . noCurrentUser
302321 }
303322
304- try await user. updatePassword ( to: password)
323+ do {
324+ try await user. updatePassword ( to: password)
325+ } catch let error as NSError {
326+ // Check if reauthentication is needed
327+ if error. domain == AuthErrorDomain,
328+ error. code == AuthErrorCode . requiresRecentLogin. rawValue ||
329+ error. code == AuthErrorCode . userTokenExpired. rawValue {
330+ try await requireReauthentication ( )
331+ } else {
332+ throw error
333+ }
334+ }
305335 }
306336}
307337
@@ -694,8 +724,19 @@ public extension AuthService {
694724 }
695725
696726 // Complete the enrollment
697- try await user. multiFactor. enroll ( with: assertion, displayName: displayName)
698- currentUser = auth. currentUser
727+ do {
728+ try await user. multiFactor. enroll ( with: assertion, displayName: displayName)
729+ currentUser = auth. currentUser
730+ } catch let error as NSError {
731+ // Check if reauthentication is needed
732+ if error. domain == AuthErrorDomain,
733+ error. code == AuthErrorCode . requiresRecentLogin. rawValue ||
734+ error. code == AuthErrorCode . userTokenExpired. rawValue {
735+ try await requireReauthentication ( )
736+ } else {
737+ throw error
738+ }
739+ }
699740 }
700741
701742 /// Reauthenticates with a simple provider (Google, Apple, Facebook, Twitter, etc.)
@@ -812,13 +853,24 @@ public extension AuthService {
812853
813854 let multiFactorUser = user. multiFactor
814855
815- try await multiFactorUser. unenroll ( withFactorUID: factorUid)
856+ do {
857+ try await multiFactorUser. unenroll ( withFactorUID: factorUid)
816858
817- // This is the only we to get the actual latest enrolledFactors
818- currentUser = Auth . auth ( ) . currentUser
819- let freshFactors = currentUser? . multiFactor. enrolledFactors ?? [ ]
859+ // This is the only we to get the actual latest enrolledFactors
860+ currentUser = Auth . auth ( ) . currentUser
861+ let freshFactors = currentUser? . multiFactor. enrolledFactors ?? [ ]
820862
821- return freshFactors
863+ return freshFactors
864+ } catch let error as NSError {
865+ // Check if reauthentication is needed
866+ if error. domain == AuthErrorDomain,
867+ error. code == AuthErrorCode . requiresRecentLogin. rawValue ||
868+ error. code == AuthErrorCode . userTokenExpired. rawValue {
869+ try await requireReauthentication ( )
870+ } else {
871+ throw error
872+ }
873+ }
822874 }
823875
824876 // MARK: - Account Conflict Helper Methods
0 commit comments