Skip to content

Commit

Permalink
Merge pull request #31522 from Expensify/Rory-FixOldDotTransition
Browse files Browse the repository at this point in the history
[CP Staging] Prevent infinite spinner in transition page

(cherry picked from commit eeece25)
  • Loading branch information
mountiny authored and OSBotify committed Nov 20, 2023
1 parent ed40b25 commit 1c7dffe
Showing 1 changed file with 41 additions and 25 deletions.
66 changes: 41 additions & 25 deletions src/pages/LogOutPreviousUserPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 <FullScreenLoadingIndicator />;
}
Expand All @@ -57,6 +70,9 @@ LogOutPreviousUserPage.defaultProps = defaultProps;
LogOutPreviousUserPage.displayName = 'LogOutPreviousUserPage';

export default withOnyx({
account: {
key: ONYXKEYS.ACCOUNT,
},
session: {
key: ONYXKEYS.SESSION,
},
Expand Down

0 comments on commit 1c7dffe

Please sign in to comment.