diff --git a/src/libs/Navigation/guards/OnboardingGuard.ts b/src/libs/Navigation/guards/OnboardingGuard.ts index c7a731798167..1a2960ca9a81 100644 --- a/src/libs/Navigation/guards/OnboardingGuard.ts +++ b/src/libs/Navigation/guards/OnboardingGuard.ts @@ -150,7 +150,9 @@ function isCurrentlyOnTwoFactorSetupRoute(state: NavigationState): boolean { } function shouldPreventReset(state: NavigationState, action: NavigationAction) { - if (action.type !== CONST.NAVIGATION_ACTIONS.RESET || !action?.payload) { + // Browser back that pops the last history entry dispatches GO_BACK instead of RESET, and it reaches the root only when it would leave onboarding + const isGoBack = action.type === CONST.NAVIGATION.ACTION_TYPE.GO_BACK; + if (!isGoBack && (action.type !== CONST.NAVIGATION_ACTIONS.RESET || !action?.payload)) { return false; } diff --git a/tests/unit/Navigation/guards/OnboardingGuard.test.ts b/tests/unit/Navigation/guards/OnboardingGuard.test.ts index 990fa7d497ad..5dfcb87f27e0 100644 --- a/tests/unit/Navigation/guards/OnboardingGuard.test.ts +++ b/tests/unit/Navigation/guards/OnboardingGuard.test.ts @@ -139,6 +139,24 @@ describe('OnboardingGuard', () => { expect(result.reason).toBe('Cannot reset to non-onboarding screen while on onboarding'); } }); + + it('should BLOCK GO_BACK action when user is on onboarding', () => { + // Given a user who is currently on the onboarding purpose screen and has not yet completed onboarding + const onboardingState: NavigationState = { + key: 'root', + index: 0, + routeNames: [SCREENS.ONBOARDING.PURPOSE], + routes: [{key: 'purpose', name: SCREENS.ONBOARDING.PURPOSE}], + stale: false, + type: 'root', + }; + + // When the browser back button pops the last history entry, which React Navigation dispatches as GO_BACK + const result = OnboardingGuard.evaluate(onboardingState, {type: CONST.NAVIGATION.ACTION_TYPE.GO_BACK}, authenticatedContext); + + // Then the action should be blocked because going back from onboarding would close the modal without finishing it + expect(result.type).toBe('BLOCK'); + }); }); describe('skip onboarding conditions', () => {