diff --git a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift index 0e0a6839f0..1e29c11d17 100644 --- a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift +++ b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift @@ -91,6 +91,19 @@ public enum AuthView: Hashable { case enterVerificationCode(verificationID: String, fullPhoneNumber: String) } +extension AuthView { + /// Whether the screen is part of signing in, so it is done once the user is signed in. + var isSignedOutOnly: Bool { + switch self { + case .signIn, .signUp, .emailLink, .passwordRecovery, .enterPhoneNumber, + .enterVerificationCode, .mfaResolution: + true + case .updatePassword, .mfaEnrollment, .mfaManagement: + false + } + } +} + public enum SignInOutcome: @unchecked Sendable { case mfaRequired(MFARequired) case signedIn(AuthDataResult?) @@ -169,10 +182,11 @@ public class Navigator { routes.removeAll() } - /// Removes pushed ``AuthView/signIn`` and ``AuthView/signUp`` screens, which have nothing left - /// to do once the user is signed in. - func removeEmailRoutes() { - routes.removeAll { $0 == .signIn || $0 == .signUp } + /// Removes pushed screens that only apply while signed out, such as sign-in, email link and + /// phone verification, which have nothing left to do once the user is signed in. Screens for + /// a signed-in user, such as MFA management, stay. + func removeSignedOutRoutes() { + routes.removeAll(where: \.isSignedOutOnly) } /// Leaves a pushed ``AuthView/signUp`` screen for sign-in: back to the sign-in screen under diff --git a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/AuthPickerContent.swift b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/AuthPickerContent.swift index 69b9c92a9e..6400c6ebd9 100644 --- a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/AuthPickerContent.swift +++ b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/AuthPickerContent.swift @@ -98,11 +98,11 @@ public struct AuthPickerContent< .environment(authService) } .interactiveDismissDisabled(authService.configuration.interactiveDismissEnabled) - // Close pushed email screens however the user got signed in, including account - // conflicts the library resolves by signing in for them. + // Close pushed sign-in screens however the user got signed in, including email links + // and account conflicts the library resolves by signing in for them. .onChange(of: authService.authenticationState) { _, state in if state == .authenticated { - authService.navigator.removeEmailRoutes() + authService.navigator.removeSignedOutRoutes() } } // Apply account conflict handling at NavigationStack level diff --git a/e2eTest/FirebaseSwiftUIExample/FirebaseSwiftUIExampleTests/EmailRouteTests.swift b/e2eTest/FirebaseSwiftUIExample/FirebaseSwiftUIExampleTests/EmailRouteTests.swift index 0aaed10e9c..9b1d0f3756 100644 --- a/e2eTest/FirebaseSwiftUIExample/FirebaseSwiftUIExampleTests/EmailRouteTests.swift +++ b/e2eTest/FirebaseSwiftUIExample/FirebaseSwiftUIExampleTests/EmailRouteTests.swift @@ -26,15 +26,31 @@ struct EmailRouteTests { private let emailOption = LegacySignInOption(id: EmailAuthProviderID, displayName: "Email") @Test - func removeEmailRoutesKeepsOtherRoutes() { + func removeSignedOutRoutesRemovesEverySignInScreen() { let navigator = Navigator() - navigator.push(.signIn) - navigator.push(.signUp) - navigator.push(.passwordRecovery) + navigator.routes = [ + .signIn, + .signUp, + .emailLink, + .passwordRecovery, + .enterPhoneNumber, + .enterVerificationCode(verificationID: "id", fullPhoneNumber: "+15551234567"), + .mfaResolution(MFARequired(hints: [.totp(displayName: nil, uid: "uid")])), + ] + + navigator.removeSignedOutRoutes() + + #expect(navigator.routes.isEmpty) + } - navigator.removeEmailRoutes() + @Test + func removeSignedOutRoutesKeepsSignedInScreensInOrder() { + let navigator = Navigator() + navigator.routes = [.mfaManagement, .emailLink, .mfaEnrollment, .updatePassword] - #expect(navigator.routes == [.passwordRecovery]) + navigator.removeSignedOutRoutes() + + #expect(navigator.routes == [.mfaManagement, .mfaEnrollment, .updatePassword]) } @Test