Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
Expand Down Expand Up @@ -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)
}
Comment on lines +188 to 190

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

To ensure thread safety and full compatibility with Swift 6 strict concurrency, the Navigator class should be isolated to the @MainActor. Since Navigator is @Observable, manages UI navigation state, and is directly bound to a NavigationStack in AuthPickerContent, mutating its routes property from non-main actors can lead to data races and runtime warnings.


/// Leaves a pushed ``AuthView/signUp`` screen for sign-in: back to the sign-in screen under
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading