Skip to content

feat(auth): make each MFA enrollment step a real navigation destination - #2467

Open
demolaf wants to merge 1 commit into
feat/auth-per-step-nav-destinationsfrom
feat/mfa-enrollment-per-step-nav
Open

feat(auth): make each MFA enrollment step a real navigation destination#2467
demolaf wants to merge 1 commit into
feat/auth-per-step-nav-destinationsfrom
feat/mfa-enrollment-per-step-nav

Conversation

@demolaf

@demolaf demolaf commented Sep 1, 2026

Copy link
Copy Markdown
Member

MFA enrollment kept its four steps in local composable state, so onBackClick hand-rolled a back stack by inspecting the selected factor, and backing out of SMS enrollment discarded the phone number the user had typed. Each step is now its own destination, so system back walks them and entered data survives.

Cross-step state moves into a MfaEnrollmentFlowState holder above the NavHost, with hand-written Savers so the SMS session and selected country survive Activity recreation. A TOTP secret can't survive one — the SDK's TotpSecret is an interface with no public reconstruction path — so a lost secret is now regenerated with a fresh QR code and an explanatory message, rather than leaving the user on an opaque "No TOTP secret available" error.

  • MfaEnrollmentDestinations.kt: shared graph extension registering the four steps, plus mfaEnrollmentStartStep so a single-allowed-factor config resolves its start step at flow entry instead of auto-advancing past SelectFactor.
  • MfaEnrollmentScreen: gained step/onNavigateToStep/onNavigateBack/flowState as a required set (a partial subset throws at composition); a null step preserves today's standalone behaviour.

Added MfaEnrollmentRouteNavigationTest, MfaEnrollmentFlowStateRestorationTest and MfaEnrollmentTotpRegenerationTest — data preservation across step switches and recreation, back-stack ordering, and TOTP regeneration.

Usage

val flowState = rememberMfaEnrollmentFlowState()
NavHost(...) {
    mfaEnrollmentDestinations(navController, configuration, /**/ flowState = flowState)
}

Maintainer note: Fixes internal CPRN-403

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request refactors the MFA enrollment flow to host steps as real navigation destinations, ensuring state survives step transitions and Activity recreation. This is achieved by introducing MfaEnrollmentFlowState and custom savers for CountryData and SmsEnrollmentSession. Feedback on the changes highlights a potential security risk of cross-user state leakage, suggesting that rememberMfaEnrollmentFlowState be keyed on the user's ID. Additionally, it is recommended to wrap a composition-time popBackStack() call in a LaunchedEffect to avoid side-effects during composition.

Comment on lines +177 to +179
} else {
navController.popBackStack()
}

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.

medium

Calling navController.popBackStack() directly during composition is a side-effect and can cause runtime issues or unpredictable navigation behavior. It should be wrapped in a LaunchedEffect to execute safely after composition. Note: You will need to import androidx.compose.runtime.LaunchedEffect at the top of the file.

            } else {
                LaunchedEffect(Unit) {
                    navController.popBackStack()
                }
            }

@demolaf demolaf Sep 2, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Valid, fixing. EmailAuthDestinations.kt:84-89 already has the right shape — LaunchedEffect(Unit) plus a spinner — so this site was inconsistent with its sibling.

Worse than described, too: each pop made the next destination compose with a null user and pop again, so it drained the whole back stack rather than navigating once during composition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant