From 1c7dffe9cb0e13f034333204d3d2551b7b635878 Mon Sep 17 00:00:00 2001 From: Vit Horacek <36083550+mountiny@users.noreply.github.com> Date: Mon, 20 Nov 2023 16:54:04 +0000 Subject: [PATCH] Merge pull request #31522 from Expensify/Rory-FixOldDotTransition [CP Staging] Prevent infinite spinner in transition page (cherry picked from commit eeece25f0f8b3a70082b8ba39aad3d0d2cc04ef0) --- src/pages/LogOutPreviousUserPage.js | 66 ++++++++++++++++++----------- 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/src/pages/LogOutPreviousUserPage.js b/src/pages/LogOutPreviousUserPage.js index 8fd0ec144a1f..df38c28e561a 100644 --- a/src/pages/LogOutPreviousUserPage.js +++ b/src/pages/LogOutPreviousUserPage.js @@ -5,10 +5,17 @@ import {Linking} from 'react-native'; import {withOnyx} from 'react-native-onyx'; import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator'; import * as SessionUtils from '@libs/SessionUtils'; +import Navigation from '@navigation/Navigation'; import * as Session from '@userActions/Session'; import ONYXKEYS from '@src/ONYXKEYS'; const propTypes = { + /** The details about the account that the user is signing in with */ + account: PropTypes.shape({ + /** Whether the account data is loading */ + isLoading: PropTypes.bool, + }), + /** The data about the current session which will be set once the user is authenticated and we return to this component as an AuthScreen */ session: PropTypes.shape({ /** The user's email for the current session */ @@ -17,37 +24,43 @@ const propTypes = { }; const defaultProps = { + account: { + isLoading: false, + }, session: { email: null, }, }; function LogOutPreviousUserPage(props) { - useEffect( - () => { - Linking.getInitialURL().then((transitionURL) => { - const sessionEmail = props.session.email; - const isLoggingInAsNewUser = SessionUtils.isLoggingInAsNewUser(transitionURL, sessionEmail); - - if (isLoggingInAsNewUser) { - Session.signOutAndRedirectToSignIn(); - } - - // We need to signin and fetch a new authToken, if a user was already authenticated in NewDot, and was redirected to OldDot - // and their authToken stored in Onyx becomes invalid. - // This workflow is triggered while setting up VBBA. User is redirected from NewDot to OldDot to set up 2FA, and then redirected back to NewDot - // On Enabling 2FA, authToken stored in Onyx becomes expired and hence we need to fetch new authToken - const shouldForceLogin = lodashGet(props, 'route.params.shouldForceLogin', '') === 'true'; - if (shouldForceLogin) { - const email = lodashGet(props, 'route.params.email', ''); - const shortLivedAuthToken = lodashGet(props, 'route.params.shortLivedAuthToken', ''); - Session.signInWithShortLivedAuthToken(email, shortLivedAuthToken); - } - }); - }, - // eslint-disable-next-line react-hooks/exhaustive-deps - [], - ); + useEffect(() => { + Linking.getInitialURL().then((transitionURL) => { + const sessionEmail = props.session.email; + const isLoggingInAsNewUser = SessionUtils.isLoggingInAsNewUser(transitionURL, sessionEmail); + + if (isLoggingInAsNewUser) { + Session.signOutAndRedirectToSignIn(); + } + + // We need to signin and fetch a new authToken, if a user was already authenticated in NewDot, and was redirected to OldDot + // and their authToken stored in Onyx becomes invalid. + // This workflow is triggered while setting up VBBA. User is redirected from NewDot to OldDot to set up 2FA, and then redirected back to NewDot + // On Enabling 2FA, authToken stored in Onyx becomes expired and hence we need to fetch new authToken + const shouldForceLogin = lodashGet(props, 'route.params.shouldForceLogin', '') === 'true'; + if (shouldForceLogin) { + const email = lodashGet(props, 'route.params.email', ''); + const shortLivedAuthToken = lodashGet(props, 'route.params.shortLivedAuthToken', ''); + Session.signInWithShortLivedAuthToken(email, shortLivedAuthToken); + } + + const exitTo = lodashGet(props, 'route.params.exitTo', ''); + if (exitTo && !props.account.isLoading && !isLoggingInAsNewUser) { + Navigation.isNavigationReady().then(() => { + Navigation.navigate(exitTo); + }); + } + }); + }, [props]); return ; } @@ -57,6 +70,9 @@ LogOutPreviousUserPage.defaultProps = defaultProps; LogOutPreviousUserPage.displayName = 'LogOutPreviousUserPage'; export default withOnyx({ + account: { + key: ONYXKEYS.ACCOUNT, + }, session: { key: ONYXKEYS.SESSION, },