@@ -218,16 +218,13 @@ public final class AuthService {
218218
219219 try await user. link ( with: credentials)
220220 updateAuthenticationState ( )
221- } catch let error as NSError {
221+ } catch {
222222 authenticationState = . unauthenticated
223223
224- // Check if reauthentication is needed
225- if error. domain == AuthErrorDomain,
226- error. code == AuthErrorCode . requiresRecentLogin. rawValue ||
227- error. code == AuthErrorCode . userTokenExpired. rawValue {
228- try await requireReauthentication ( )
229- }
224+ // Check for reauthentication errors first
225+ try await handleErrorWithReauthCheck ( error: error)
230226
227+ // If not a reauth error, check for conflicts
231228 // Possible conflicts from user.link():
232229 // - credentialAlreadyInUse: credential is already linked to another account
233230 // - emailAlreadyInUse: email from credential is already used by another account
@@ -304,15 +301,9 @@ public extension AuthService {
304301
305302 do {
306303 try await user. delete ( )
307- } catch let error as NSError {
308- // Check if reauthentication is needed
309- if error. domain == AuthErrorDomain,
310- error. code == AuthErrorCode . requiresRecentLogin. rawValue ||
311- error. code == AuthErrorCode . userTokenExpired. rawValue {
312- try await requireReauthentication ( )
313- } else {
314- throw error
315- }
304+ } catch {
305+ try await handleErrorWithReauthCheck ( error: error)
306+ throw error // If we reach here, it wasn't a reauth error, so rethrow
316307 }
317308 }
318309
@@ -323,15 +314,9 @@ public extension AuthService {
323314
324315 do {
325316 try await user. updatePassword ( to: password)
326- } catch let error as NSError {
327- // Check if reauthentication is needed
328- if error. domain == AuthErrorDomain,
329- error. code == AuthErrorCode . requiresRecentLogin. rawValue ||
330- error. code == AuthErrorCode . userTokenExpired. rawValue {
331- try await requireReauthentication ( )
332- } else {
333- throw error
334- }
317+ } catch {
318+ try await handleErrorWithReauthCheck ( error: error)
319+ throw error // If we reach here, it wasn't a reauth error, so rethrow
335320 }
336321 }
337322}
@@ -728,15 +713,9 @@ public extension AuthService {
728713 do {
729714 try await user. multiFactor. enroll ( with: assertion, displayName: displayName)
730715 currentUser = auth. currentUser
731- } catch let error as NSError {
732- // Check if reauthentication is needed
733- if error. domain == AuthErrorDomain,
734- error. code == AuthErrorCode . requiresRecentLogin. rawValue ||
735- error. code == AuthErrorCode . userTokenExpired. rawValue {
736- try await requireReauthentication ( )
737- } else {
738- throw error
739- }
716+ } catch {
717+ try await handleErrorWithReauthCheck ( error: error)
718+ throw error // If we reach here, it wasn't a reauth error, so rethrow
740719 }
741720 }
742721
@@ -773,6 +752,19 @@ public extension AuthService {
773752 currentUser = auth. currentUser
774753 }
775754
755+ /// Checks if an error requires reauthentication and handles it appropriately
756+ /// - Parameter error: The error to check
757+ /// - Throws: Only if it's a reauthentication error (via requireReauthentication())
758+ private func handleErrorWithReauthCheck( error: Error ) async throws {
759+ if let nsError = error as NSError ? ,
760+ nsError. domain == AuthErrorDomain,
761+ nsError. code == AuthErrorCode . requiresRecentLogin. rawValue ||
762+ nsError. code == AuthErrorCode . userTokenExpired. rawValue {
763+ try await requireReauthentication ( )
764+ }
765+ // If not a reauth error, return normally so caller can handle it
766+ }
767+
776768 /// Internal helper to create reauth context and throw appropriate error
777769 /// - Throws: Appropriate `AuthServiceError` based on the provider type
778770 private func requireReauthentication( ) async throws -> Never {
@@ -866,15 +858,9 @@ public extension AuthService {
866858 let freshFactors = currentUser? . multiFactor. enrolledFactors ?? [ ]
867859
868860 return freshFactors
869- } catch let error as NSError {
870- // Check if reauthentication is needed
871- if error. domain == AuthErrorDomain,
872- error. code == AuthErrorCode . requiresRecentLogin. rawValue ||
873- error. code == AuthErrorCode . userTokenExpired. rawValue {
874- try await requireReauthentication ( )
875- } else {
876- throw error
877- }
861+ } catch {
862+ try await handleErrorWithReauthCheck ( error: error)
863+ throw error // If we reach here, it wasn't a reauth error, so rethrow
878864 }
879865 }
880866
0 commit comments