diff --git a/src/libs/Navigation/types.ts b/src/libs/Navigation/types.ts index 71864582812f..7e6a13ec7ffc 100644 --- a/src/libs/Navigation/types.ts +++ b/src/libs/Navigation/types.ts @@ -462,6 +462,7 @@ type PublicScreensParamList = { [SCREENS.VALIDATE_LOGIN]: { accountID: string; validateCode: string; + exitTo?: Routes | HybridAppRoute; }; [SCREENS.UNLINK_LOGIN]: { accountID?: string; diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index d000d5ebfbec..7416b4f07e5e 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -1,7 +1,7 @@ import throttle from 'lodash/throttle'; import type {ChannelAuthorizationData} from 'pusher-js/types/src/core/auth/options'; import type {ChannelAuthorizationCallback} from 'pusher-js/with-encryption'; -import {Linking} from 'react-native'; +import {InteractionManager, Linking, NativeModules} from 'react-native'; import type {OnyxUpdate} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import type {ValueOf} from 'type-fest'; @@ -45,6 +45,7 @@ import * as Welcome from '@userActions/Welcome'; import CONFIG from '@src/CONFIG'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; +import type {HybridAppRoute, Route as Routes} from '@src/ROUTES'; import ROUTES from '@src/ROUTES'; import SCREENS from '@src/SCREENS'; import type Credentials from '@src/types/onyx/Credentials'; @@ -504,11 +505,6 @@ function signInWithValidateCode(accountID: number, code: string, twoFactorAuthCo }); } -function signInWithValidateCodeAndNavigate(accountID: number, validateCode: string, twoFactorAuthCode = '') { - signInWithValidateCode(accountID, validateCode, twoFactorAuthCode); - Navigation.navigate(ROUTES.HOME); -} - /** * Initializes the state of the automatic authentication when the user clicks on a magic link. * @@ -855,6 +851,26 @@ function waitForUserSignIn(): Promise { }); } +function handleExitToNavigation(exitTo: Routes | HybridAppRoute) { + InteractionManager.runAfterInteractions(() => { + waitForUserSignIn().then(() => { + Navigation.waitForProtectedRoutes().then(() => { + const url = NativeModules.HybridAppModule ? Navigation.parseHybridAppUrl(exitTo) : exitTo; + Navigation.navigate(url, CONST.NAVIGATION.TYPE.FORCED_UP); + }); + }); + }); +} + +function signInWithValidateCodeAndNavigate(accountID: number, validateCode: string, twoFactorAuthCode = '', exitTo?: Routes | HybridAppRoute) { + signInWithValidateCode(accountID, validateCode, twoFactorAuthCode); + if (exitTo) { + handleExitToNavigation(exitTo); + } else { + Navigation.navigate(ROUTES.HOME); + } +} + /** * check if the route can be accessed by anonymous user * @@ -890,6 +906,7 @@ export { checkIfActionIsAllowed, signIn, signInWithValidateCode, + handleExitToNavigation, signInWithValidateCodeAndNavigate, initAutoAuthState, signInWithShortLivedAuthToken, diff --git a/src/pages/ValidateLoginPage/index.tsx b/src/pages/ValidateLoginPage/index.tsx index 5415c359aab0..edc8b61c82b0 100644 --- a/src/pages/ValidateLoginPage/index.tsx +++ b/src/pages/ValidateLoginPage/index.tsx @@ -9,7 +9,7 @@ import type {ValidateLoginPageOnyxNativeProps, ValidateLoginPageProps} from './t function ValidateLoginPage({ route: { - params: {accountID, validateCode}, + params: {accountID, validateCode, exitTo}, }, session, }: ValidateLoginPageProps) { @@ -21,7 +21,7 @@ function ValidateLoginPage({ // because we don't want to block the user with the interstitial page. Navigation.goBack(); } else { - Session.signInWithValidateCodeAndNavigate(Number(accountID), validateCode); + Session.signInWithValidateCodeAndNavigate(Number(accountID), validateCode, '', exitTo); } }); // eslint-disable-next-line react-hooks/exhaustive-deps diff --git a/src/pages/ValidateLoginPage/index.website.tsx b/src/pages/ValidateLoginPage/index.website.tsx index 6253535e9789..7ce46ee11b14 100644 --- a/src/pages/ValidateLoginPage/index.website.tsx +++ b/src/pages/ValidateLoginPage/index.website.tsx @@ -14,7 +14,7 @@ function ValidateLoginPage({ account, credentials, route: { - params: {accountID, validateCode}, + params: {accountID, validateCode, exitTo}, }, session, }: ValidateLoginPageProps) { @@ -35,6 +35,9 @@ function ValidateLoginPage({ Session.initAutoAuthState(autoAuthState); if (isSignedIn || !login) { + if (exitTo) { + Session.handleExitToNavigation(exitTo); + } return; } @@ -45,6 +48,9 @@ function ValidateLoginPage({ useEffect(() => { if (!!login || !cachedAccountID || !is2FARequired) { + if (exitTo) { + Session.handleExitToNavigation(exitTo); + } return; } @@ -52,7 +58,7 @@ function ValidateLoginPage({ Navigation.isNavigationReady().then(() => { Navigation.goBack(); }); - }, [login, cachedAccountID, is2FARequired]); + }, [login, cachedAccountID, is2FARequired, exitTo]); return ( <>