Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
7746056
Conditionally render RequireTwoFactorAuthenticationPage based on stat…
chuckdries Dec 12, 2025
fbb2734
Allow navigation to 2FA disabled screen when 2fa is enforced
chuckdries Dec 12, 2025
3c61c62
Prevent erroneous EnableTwoFactorAuth call in 2fa device switch flow
chuckdries Dec 12, 2025
21f8c12
getCurrentUrl doesn't subscribe to updates - use useNavigationState. …
chuckdries Dec 12, 2025
581e306
Fix lint and improve comments in Navigation
chuckdries Dec 12, 2025
2b95b38
Remove unnecessary NavigationTree for RequireTwoFactorAuthenticationPage
chuckdries Dec 12, 2025
a5c176d
Run prettier
chuckdries Dec 12, 2025
fedcf48
Clean up selector that's only used once so we can more precisely memo…
chuckdries Dec 16, 2025
683a9c7
Merge remote-tracking branch 'origin/main' into chuckdries/fix-2fa-en…
chuckdries Dec 30, 2025
6002cf9
Merge remote-tracking branch 'origin/main' into chuckdries/fix-2fa-en…
chuckdries Jan 6, 2026
0c74193
Fix 2FA setup screen detection when user visits setup URL directly (o…
chuckdries Jan 6, 2026
302dd55
Hide concierge help button on 2FA setup screens
chuckdries Jan 6, 2026
0b68c64
Run prettier
chuckdries Jan 6, 2026
dd5c471
Merge remote-tracking branch 'origin/main' into chuckdries/fix-2fa-en…
chuckdries Jan 12, 2026
f9f0ced
Fix visual issues with RequireTwoFactorAuthenticationPage in mobile n…
chuckdries Jan 15, 2026
47bd748
Merge remote-tracking branch 'origin/main' into chuckdries/fix-2fa-en…
chuckdries Jan 15, 2026
839342d
Fix lint and run prettier
chuckdries Jan 15, 2026
b26cdf0
Merge remote-tracking branch 'origin/main' into chuckdries/fix-2fa-en…
chuckdries Jan 15, 2026
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
1 change: 0 additions & 1 deletion src/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ const ROUTES = {
ENABLE_PAYMENTS: 'enable-payments',
WALLET_STATEMENT_WITH_DATE: 'statements/:yearMonth',
SIGN_IN_MODAL: 'sign-in-modal',
REQUIRE_TWO_FACTOR_AUTH: '2fa-required',

BANK_ACCOUNT: 'bank-account',
BANK_ACCOUNT_VERIFY_ACCOUNT: {
Expand Down
12 changes: 0 additions & 12 deletions src/hooks/useOnboardingFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ function useOnboardingFlowRouter() {
const [sessionEmail] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: true, selector: emailSelector});
const isLoggingInAsNewSessionUser = isLoggingInAsNewUser(currentUrl, sessionEmail);
const startedOnboardingFlowRef = useRef(false);
const started2FAFlowRef = useRef(false);
const [tryNewDot, tryNewDotMetadata] = useOnyx(ONYXKEYS.NVP_TRY_NEW_DOT, {
selector: tryNewDotOnyxSelector,
canBeMissing: true,
Expand Down Expand Up @@ -79,16 +78,6 @@ function useOnboardingFlowRouter() {
return;
}

if (shouldShowRequire2FAPage) {
if (started2FAFlowRef.current) {
startedOnboardingFlowRef.current = false;
return;
}
started2FAFlowRef.current = true;
Navigation.navigate(ROUTES.REQUIRE_TWO_FACTOR_AUTH);
return;
}

if (hasBeenAddedToNudgeMigration && !isProductTrainingElementDismissed('migratedUserWelcomeModal', dismissedProductTraining)) {
const navigationState = navigationRef.getRootState();
const lastRoute = navigationState.routes.at(-1);
Expand Down Expand Up @@ -173,7 +162,6 @@ function useOnboardingFlowRouter() {
currentOnboardingPurposeSelected,
onboardingInitialPath,
isOnboardingLoading,
shouldShowRequire2FAPage,
]);

return {
Expand Down
9 changes: 8 additions & 1 deletion src/hooks/useSidePanelDisplayStatus.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {getDeepestFocusedScreenName, isTwoFactorSetupScreen} from '@libs/Navigation/Navigation';
import ONYXKEYS from '@src/ONYXKEYS';
import {hasCompletedGuidedSetupFlowSelector} from '@src/selectors/Onboarding';
import useIsAnonymousUser from './useIsAnonymousUser';
import useOnyx from './useOnyx';
import useResponsiveLayout from './useResponsiveLayout';
import useRootNavigationState from './useRootNavigationState';

/**
* Hook to get the display status of the Side Panel
Expand All @@ -16,6 +18,10 @@ function useSidePanelDisplayStatus() {
});
const isAnonymousUser = useIsAnonymousUser();
const isSidePanelVisible = isExtraLargeScreenWidth ? sidePanelNVP?.open : sidePanelNVP?.openNarrowScreen;
const isIn2FASetupFlow = useRootNavigationState((state) => {
const focusedScreenName = getDeepestFocusedScreenName(state);
return isTwoFactorSetupScreen(focusedScreenName);
});

// The Side Panel is hidden when:
// - NVP is not set or it is false
Expand All @@ -27,7 +33,8 @@ function useSidePanelDisplayStatus() {
// - Side Panel is displayed currently
// - Onboarding is not completed
// - User is anonymous (not signed in)
const shouldHideHelpButton = !shouldHideSidePanel || !isOnboardingCompleted || isAnonymousUser;
// - User is setting up 2FA - if their current token is a TWO_FACTOR_SETUP token, any messages to concierge will fail to send
const shouldHideHelpButton = !shouldHideSidePanel || !isOnboardingCompleted || isAnonymousUser || isIn2FASetupFlow;
const shouldHideSidePanelBackdrop = shouldHideSidePanel || isExtraLargeScreenWidth || shouldUseNarrowLayout;

return {
Expand Down
25 changes: 14 additions & 11 deletions src/libs/Navigation/AppNavigator/AuthScreens.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {RouteProp} from '@react-navigation/native';
import {useNavigationState} from '@react-navigation/native';
import type {StackCardInterpolationProps} from '@react-navigation/stack';
import React, {memo, useContext, useEffect, useRef, useState} from 'react';
import ComposeProviders from '@components/ComposeProviders';
Expand Down Expand Up @@ -33,7 +34,7 @@ import KeyboardShortcut from '@libs/KeyboardShortcut';
import Log from '@libs/Log';
import NavBarManager from '@libs/NavBarManager';
import getCurrentUrl from '@libs/Navigation/currentUrl';
import Navigation from '@libs/Navigation/Navigation';
import Navigation, {getDeepestFocusedScreenName, isTwoFactorSetupScreen} from '@libs/Navigation/Navigation';
import Animations, {InternalPlatformAnimations} from '@libs/Navigation/PlatformStackNavigation/navigationOptions/animation';
import type {AuthScreensParamList} from '@libs/Navigation/types';
import NetworkConnection from '@libs/NetworkConnection';
Expand Down Expand Up @@ -157,6 +158,13 @@ function AuthScreens() {
const {shouldRenderSecondaryOverlayForWideRHP, shouldRenderSecondaryOverlayForRHPOnWideRHP, shouldRenderSecondaryOverlayForRHPOnSuperWideRHP, shouldRenderTertiaryOverlay} =
useContext(WideRHPContext);

// Check if the user is currently on a 2FA setup screen
// We can't rely on useRoute in this component because we're not a child of a Navigator, so we must sift through nav state by hand
const isIn2FASetupFlow = useNavigationState((state) => {
const focusedScreenName = getDeepestFocusedScreenName(state);
return isTwoFactorSetupScreen(focusedScreenName);
});

// State to track whether the delegator's authentication is completed before displaying data
const [isDelegatorFromOldDotIsReady, setIsDelegatorFromOldDotIsReady] = useState(false);

Expand Down Expand Up @@ -259,7 +267,7 @@ function AuthScreens() {
shortcutsOverviewShortcutConfig.shortcutKey,
() => {
Modal.close(() => {
if (Navigation.isOnboardingFlow()) {
if (Navigation.isOnboardingFlow() || shouldShowRequire2FAPage) {
return;
}

Expand All @@ -281,7 +289,7 @@ function AuthScreens() {
searchShortcutConfig.shortcutKey,
() => {
Session.callFunctionIfActionIsAllowed(() => {
if (Navigation.isOnboardingFlow()) {
if (Navigation.isOnboardingFlow() || shouldShowRequire2FAPage) {
return;
}
toggleSearch();
Expand All @@ -295,7 +303,7 @@ function AuthScreens() {
const unsubscribeChatShortcut = KeyboardShortcut.subscribe(
chatShortcutConfig.shortcutKey,
() => {
if (Navigation.isOnboardingFlow()) {
if (Navigation.isOnboardingFlow() || shouldShowRequire2FAPage) {
return;
}
Session.callFunctionIfActionIsAllowed(() => Modal.close(() => Navigation.navigate(ROUTES.NEW)))();
Expand Down Expand Up @@ -655,13 +663,6 @@ function AuthScreens() {
}}
/>
)}
{shouldShowRequire2FAPage && (
<RootStack.Screen
name={SCREENS.REQUIRE_TWO_FACTOR_AUTH}
options={{...rootNavigatorScreenOptions.fullScreen, gestureEnabled: false}}
component={RequireTwoFactorAuthenticationPage}
/>
)}
<RootStack.Screen
name={SCREENS.WORKSPACE_JOIN_USER}
options={{
Expand Down Expand Up @@ -701,6 +702,8 @@ function AuthScreens() {
listeners={modalScreenListeners}
/>
</RootStack.Navigator>
{/* Full-screen 2FA enforcement overlay - blocks all interaction until 2FA is set up */}
{shouldShowRequire2FAPage && !isIn2FASetupFlow && <RequireTwoFactorAuthenticationPage />}
<SearchRouterModal />
<OpenAppFailureModal />
<PriorityModeController />
Expand Down
71 changes: 56 additions & 15 deletions src/libs/Navigation/Navigation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {findFocusedRoute, getActionFromState} from '@react-navigation/core';
import type {EventArg, NavigationAction, NavigationContainerEventMap, NavigationState} from '@react-navigation/native';
import type {EventArg, NavigationAction, NavigationContainerEventMap, NavigationState, PartialState} from '@react-navigation/native';
import {CommonActions, getPathFromState, StackActions} from '@react-navigation/native';
import {Str} from 'expensify-common';
// eslint-disable-next-line you-dont-need-lodash-underscore/omit
Expand Down Expand Up @@ -48,12 +48,14 @@ import type {
State,
} from './types';

// Routes which are part of the flow to set up 2FA
const SET_UP_2FA_ROUTES = new Set<Route>([
ROUTES.REQUIRE_TWO_FACTOR_AUTH,
ROUTES.SETTINGS_2FA_ROOT.getRoute(ROUTES.REQUIRE_TWO_FACTOR_AUTH),
ROUTES.SETTINGS_2FA_VERIFY.getRoute(ROUTES.REQUIRE_TWO_FACTOR_AUTH),
ROUTES.SETTINGS_2FA_SUCCESS.getRoute(ROUTES.REQUIRE_TWO_FACTOR_AUTH),
// Screens which are part of the 2FA setup flow - used to determine when to hide the RequireTwoFactorAuthOverlay
const SET_UP_2FA_SCREENS = new Set<string>([
SCREENS.TWO_FACTOR_AUTH.ROOT,
SCREENS.TWO_FACTOR_AUTH.VERIFY,
SCREENS.TWO_FACTOR_AUTH.VERIFY_ACCOUNT,
SCREENS.TWO_FACTOR_AUTH.SUCCESS,
SCREENS.TWO_FACTOR_AUTH.DISABLED,
SCREENS.TWO_FACTOR_AUTH.DISABLE,
]);

let account: OnyxEntry<Account>;
Expand All @@ -75,6 +77,10 @@ Onyx.connectWithoutView({
},
});

function isTwoFactorSetupScreen(screen: string | undefined): boolean {
return screen ? SET_UP_2FA_SCREENS.has(screen) : false;
}

function shouldShowRequire2FAPage() {
return !!account?.needsTwoFactorAuthSetup && !account?.requiresTwoFactorAuth;
}
Expand Down Expand Up @@ -103,21 +109,56 @@ function getShouldPopToSidebar() {
return shouldPopToSidebar;
}

/**
* Recursively get the deepest focused screen name from the navigation state.
* Unlike findFocusedRoute, this also handles the case where the nested navigator
* hasn't been mounted yet and the target screen is in params instead of state.
*/
function getDeepestFocusedScreenName(route: NavigationRoute | NavigationState | PartialState<NavigationState> | undefined): string | undefined {
if (!route) {
return undefined;
}

// NavigationState case - has routes array
if ('routes' in route && Array.isArray(route.routes)) {
// When routes array is just one item, the index key is omitted
let focusedRoute = route.routes[0];
if ('index' in route && typeof route.index === 'number') {
focusedRoute = route.routes[route.index];
}
return getDeepestFocusedScreenName(focusedRoute);
}

// Route with nested state case
if ('state' in route && route.state) {
return getDeepestFocusedScreenName(route.state);
}

// Route with params.screen case (initial navigation before sidebar navigator mounts)
if ('params' in route && route.params && typeof route.params === 'object' && 'screen' in route.params) {
const params = route.params as {screen?: string; params?: Record<string, unknown>};
if (params.screen) {
return getDeepestFocusedScreenName({name: params.screen, params: params.params});
}
}

// Leaf route - return the name
if ('name' in route) {
return route.name;
}

return undefined;
}

type CanNavigateParams = {
route?: Route;
backToRoute?: Route;
};

/**
* Checks if the route can be navigated to based on whether the navigation ref is ready and if 2FA is required to be set up.
* Checks if navigation is ready.
*/
function canNavigate(methodName: string, params: CanNavigateParams = {}): boolean {
// Block navigation if 2FA is required and the targetRoute is not part of the flow to enable 2FA
const targetRoute = params.route ?? params.backToRoute;
if (shouldShowRequire2FAPage() && targetRoute && !SET_UP_2FA_ROUTES.has(targetRoute)) {
Log.info(`[Navigation] Blocked navigation because 2FA is required to be set up to access route: ${targetRoute}`);
return false;
}
if (navigationRef.isReady()) {
return true;
}
Expand Down Expand Up @@ -892,4 +933,4 @@ export default {
navigateBackToLastSuperWideRHPScreen,
};

export {navigationRef};
export {navigationRef, getDeepestFocusedScreenName, isTwoFactorSetupScreen, shouldShowRequire2FAPage};
5 changes: 0 additions & 5 deletions src/libs/Navigation/NavigationRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,6 @@ function NavigationRoot({authenticated, lastVisitedPath, initialUrl, onReady}: N
return;
}

const shouldShowRequire2FAPage = !!account?.needsTwoFactorAuthSetup && !account.requiresTwoFactorAuth;
if (shouldShowRequire2FAPage) {
return getAdaptedStateFromPath(ROUTES.REQUIRE_TWO_FACTOR_AUTH, linkingConfig.config);
}

const isTransitioning = path?.includes(ROUTES.TRANSITION_BETWEEN_APPS);

// If we have a transition URL, don't restore last visited path - let React Navigation handle it
Expand Down
1 change: 0 additions & 1 deletion src/libs/Navigation/linkingConfig/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const config: LinkingOptions<RootNavigatorParamList>['config'] = {
[SCREENS.TRANSACTION_RECEIPT]: ROUTES.TRANSACTION_RECEIPT.route,
[SCREENS.MONEY_REQUEST.RECEIPT_PREVIEW]: ROUTES.MONEY_REQUEST_RECEIPT_PREVIEW.route,
[SCREENS.WORKSPACE_JOIN_USER]: ROUTES.WORKSPACE_JOIN_USER.route,
[SCREENS.REQUIRE_TWO_FACTOR_AUTH]: ROUTES.REQUIRE_TWO_FACTOR_AUTH,
[SCREENS.WORKSPACES_LIST]: {
path: ROUTES.WORKSPACES_LIST.route,
exact: true,
Expand Down
61 changes: 34 additions & 27 deletions src/pages/RequireTwoFactorAuthenticationPage.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {isUserValidatedSelector} from '@selectors/Account';
import React, {useCallback} from 'react';
import {View} from 'react-native';
import {StyleSheet, View} from 'react-native';
import type {OnyxCollection} from 'react-native-onyx';
import Button from '@components/Button';
import FocusTrapForModal from '@components/FocusTrap/FocusTrapForModal';
import Icon from '@components/Icon';
import ScreenWrapper from '@components/ScreenWrapper';
import Text from '@components/Text';
import {useMemoizedLazyIllustrations} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
Expand Down Expand Up @@ -43,39 +43,46 @@ function RequireTwoFactorAuthenticationPage() {

const handleOnPress = useCallback(() => {
if (isUserValidated) {
Navigation.navigate(ROUTES.SETTINGS_2FA_ROOT.getRoute(ROUTES.REQUIRE_TWO_FACTOR_AUTH));
Navigation.navigate(ROUTES.SETTINGS_2FA_ROOT.getRoute());
return;
}
Navigation.navigate(ROUTES.SETTINGS_2FA_VERIFY_ACCOUNT.getRoute({backTo: ROUTES.REQUIRE_TWO_FACTOR_AUTH, forwardTo: ROUTES.SETTINGS_2FA_ROOT.getRoute()}));
Navigation.navigate(ROUTES.SETTINGS_2FA_VERIFY_ACCOUNT.getRoute({forwardTo: ROUTES.SETTINGS_2FA_ROOT.getRoute()}));
}, [isUserValidated]);

return (
<ScreenWrapper testID="RequireTwoFactorAuthenticationPage">
<View style={styles.twoFARequiredContainer}>
<View style={[styles.twoFAIllustration, styles.alignItemsCenter]}>
<Icon
src={illustrations.Encryption}
width={variables.twoFAIconHeight}
height={variables.twoFAIconHeight}
/>
</View>
<View style={[styles.mt2, styles.mh5, styles.dFlex, styles.alignItemsCenter]}>
<View style={styles.mb5}>
<Text style={[styles.textHeadlineH1, styles.textAlignCenter, styles.mv2]}>{translate('twoFactorAuth.twoFactorAuthIsRequiredForAdminsHeader')}</Text>
<Text style={[styles.textSupporting, styles.textAlignCenter]}>
{translate(is2FARequiredBecauseOfXero ? 'twoFactorAuth.twoFactorAuthIsRequiredXero' : 'twoFactorAuth.twoFactorAuthIsRequiredCompany')}
</Text>
<FocusTrapForModal active>
<View
style={[StyleSheet.absoluteFill, styles.twoFARequiredOverlay]}
testID="RequireTwoFactorAuthenticationPage"
>
<View style={[styles.flex1, styles.appBG]}>
<View style={styles.twoFARequiredContainer}>
<View style={[styles.twoFAIllustration, styles.alignItemsCenter]}>
<Icon
src={illustrations.Encryption}
width={variables.twoFAIconHeight}
height={variables.twoFAIconHeight}
/>
</View>
<View style={[styles.mt2, styles.mh5, styles.dFlex, styles.alignItemsCenter]}>
<View style={styles.mb5}>
<Text style={[styles.textHeadlineH1, styles.textAlignCenter, styles.mv2]}>{translate('twoFactorAuth.twoFactorAuthIsRequiredForAdminsHeader')}</Text>
<Text style={[styles.textSupporting, styles.textAlignCenter]}>
{translate(is2FARequiredBecauseOfXero ? 'twoFactorAuth.twoFactorAuthIsRequiredXero' : 'twoFactorAuth.twoFactorAuthIsRequiredCompany')}
</Text>
</View>
<Button
large
success
pressOnEnter
onPress={handleOnPress}
text={translate('twoFactorAuth.enableTwoFactorAuth')}
/>
</View>
</View>
<Button
large
success
pressOnEnter
onPress={handleOnPress}
text={translate('twoFactorAuth.enableTwoFactorAuth')}
/>
</View>
</View>
</ScreenWrapper>
</FocusTrapForModal>
);
}

Expand Down
11 changes: 10 additions & 1 deletion src/pages/settings/Security/TwoFactorAuth/CopyCodesPage.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {useIsFocused} from '@react-navigation/native';
import React, {useEffect, useState} from 'react';
import {View} from 'react-native';
import ActivityIndicator from '@components/ActivityIndicator';
Expand Down Expand Up @@ -38,6 +39,7 @@ function CopyCodesPage({route}: TwoFactorAuthPageProps) {
// eslint-disable-next-line rulesdir/prefer-shouldUseNarrowLayout-instead-of-isSmallScreenWidth
const {isExtraSmallScreenWidth, isSmallScreenWidth} = useResponsiveLayout();
const [error, setError] = useState('');
const isFocused = useIsFocused();

const [account, accountMetadata] = useOnyx(ONYXKEYS.ACCOUNT, {canBeMissing: true});

Expand All @@ -53,9 +55,16 @@ function CopyCodesPage({route}: TwoFactorAuthPageProps) {
if (isLoadingOnyxValue(accountMetadata) || account?.requiresTwoFactorAuth || account?.recoveryCodes || !isUserValidated) {
return;
}

// This screen is rendered underneath other 2FA screens. We don't want it making
// API calls in the background in response to state updates
if (!isFocused) {
return;
}

toggleTwoFactorAuth(true);
// eslint-disable-next-line react-hooks/exhaustive-deps -- We want to run this when component mounts
}, [isUserValidated, accountMetadata.status]);
}, [isUserValidated, accountMetadata.status, isFocused]);

return (
<TwoFactorAuthWrapper
Expand Down
Loading