@@ -301,19 +301,15 @@ public extension AuthService {
301301 throw AuthServiceError . noCurrentUser
302302 }
303303
304- try await withReauthenticationIfNeeded ( on: user) {
305- try await user. delete ( )
306- }
304+ try await user. delete ( )
307305 }
308306
309307 func updatePassword( to password: String ) async throws {
310308 guard let user = auth. currentUser else {
311309 throw AuthServiceError . noCurrentUser
312310 }
313311
314- try await withReauthenticationIfNeeded ( on: user) {
315- try await user. updatePassword ( to: password)
316- }
312+ try await user. updatePassword ( to: password)
317313 }
318314}
319315
@@ -708,44 +704,18 @@ public extension AuthService {
708704 }
709705
710706 // Complete the enrollment
711- try await withReauthenticationIfNeeded ( on: user) {
712- try await user. multiFactor. enroll ( with: assertion, displayName: displayName)
713- }
707+ try await user. multiFactor. enroll ( with: assertion, displayName: displayName)
714708 currentUser = auth. currentUser
715709 }
716710
717- /// Gets the provider ID that was used for the current sign-in session
718- private func getCurrentSignInProvider( ) async throws -> String {
711+ /// Reauthenticates the current user with their sign-in provider
712+ /// - Throws: `AuthServiceError.phoneReauthenticationRequired` for phone auth users
713+ /// - Throws: `AuthServiceError.providerNotFound` if provider is not configured
714+ func reauthenticate( ) async throws {
719715 guard let user = currentUser else {
720716 throw AuthServiceError . noCurrentUser
721717 }
722718
723- // Get the ID token result which contains the signInProvider claim
724- let tokenResult = try await user. getIDTokenResult ( forcingRefresh: false )
725-
726- // The signInProvider property tells us which provider was used for this session
727- let signInProvider = tokenResult. signInProvider
728-
729- // If signInProvider is not empty, use it
730- if !signInProvider. isEmpty {
731- return signInProvider
732- }
733-
734- // Fallback: if signInProvider is empty, try to infer from providerData
735- // Prefer non-password providers as they're more specific
736- let providerId = user. providerData. first ( where: { $0. providerID != " password " } ) ? . providerID
737- ?? user. providerData. first? . providerID
738-
739- guard let providerId = providerId else {
740- throw AuthServiceError . reauthenticationRequired (
741- " Unable to determine sign-in provider for reauthentication "
742- )
743- }
744-
745- return providerId
746- }
747-
748- func reauthenticateCurrentUser( on user: User ) async throws {
749719 // Get the provider from the token instead of stored credential
750720 let providerId = try await getCurrentSignInProvider ( )
751721
@@ -761,36 +731,52 @@ public extension AuthService {
761731 }
762732
763733 let credential = try await emailProvider. createReauthCredential ( email: email)
764- _ = try await user. reauthenticate ( with: credential)
734+ try await user. reauthenticate ( with: credential)
765735 } else if providerId == PhoneAuthProviderID {
766- // Phone auth requires manual reauthentication via sign out and sign in otherwise it will take
767- // the user out of the existing flow
768- throw AuthServiceError . reauthenticationRequired (
769- " Phone authentication requires you to sign out and sign in again to continue "
770- )
736+ guard let phoneNumber = user. phoneNumber else {
737+ throw AuthServiceError . invalidCredentials ( " User does not have a phone number " )
738+ }
739+
740+ // Throw error with context for phone reauthentication
741+ throw AuthServiceError . phoneReauthenticationRequired ( phoneNumber: phoneNumber)
771742 } else if let matchingProvider = providers. first ( where: { $0. id == providerId } ) ,
772743 let credentialProvider = matchingProvider. provider as? CredentialAuthProviderSwift {
773744 let credential = try await credentialProvider. createAuthCredential ( )
774- _ = try await user. reauthenticate ( with: credential)
745+ try await user. reauthenticate ( with: credential)
775746 } else {
776747 throw AuthServiceError . providerNotFound ( " No provider found for \( providerId) " )
777748 }
778749 }
779750
780- private func withReauthenticationIfNeeded( on user: User ,
781- operation: ( ) async throws -> Void ) async throws {
782- do {
783- try await operation ( )
784- } catch let error as NSError {
785- if error. domain == AuthErrorDomain,
786- error. code == AuthErrorCode . requiresRecentLogin. rawValue || error. code == AuthErrorCode
787- . userTokenExpired. rawValue {
788- try await reauthenticateCurrentUser ( on: user)
789- try await operation ( )
790- } else {
791- throw error
792- }
751+ /// Gets the provider ID that was used for the current sign-in session
752+ func getCurrentSignInProvider( ) async throws -> String {
753+ guard let user = currentUser else {
754+ throw AuthServiceError . noCurrentUser
755+ }
756+
757+ // Get the ID token result which contains the signInProvider claim
758+ let tokenResult = try await user. getIDTokenResult ( forcingRefresh: false )
759+
760+ // The signInProvider property tells us which provider was used for this session
761+ let signInProvider = tokenResult. signInProvider
762+
763+ // If signInProvider is not empty, use it
764+ if !signInProvider. isEmpty {
765+ return signInProvider
766+ }
767+
768+ // Fallback: if signInProvider is empty, try to infer from providerData
769+ // Prefer non-password providers as they're more specific
770+ let providerId = user. providerData. first ( where: { $0. providerID != " password " } ) ? . providerID
771+ ?? user. providerData. first? . providerID
772+
773+ guard let providerId = providerId else {
774+ throw AuthServiceError . reauthenticationRequired (
775+ " Unable to determine sign-in provider for reauthentication "
776+ )
793777 }
778+
779+ return providerId
794780 }
795781
796782 func unenrollMFA( _ factorUid: String ) async throws -> [ MultiFactorInfo ] {
@@ -800,9 +786,7 @@ public extension AuthService {
800786
801787 let multiFactorUser = user. multiFactor
802788
803- try await withReauthenticationIfNeeded ( on: user) {
804- try await multiFactorUser. unenroll ( withFactorUID: factorUid)
805- }
789+ try await multiFactorUser. unenroll ( withFactorUID: factorUid)
806790
807791 // This is the only we to get the actual latest enrolledFactors
808792 currentUser = Auth . auth ( ) . currentUser
0 commit comments