From bfba944bdeb7522ea855b2125c4fb45eeae98a76 Mon Sep 17 00:00:00 2001 From: Agata Kosior Date: Fri, 6 Oct 2023 09:56:56 +0200 Subject: [PATCH 1/8] fix: fix transitioning issue from old dot --- .../Navigation/AppNavigator/AuthScreens.js | 12 +++++++ src/libs/actions/App.js | 3 -- src/pages/LogOutPreviousUserPage.js | 33 +++++-------------- 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index 428550a43aa8..e0db8905a2b8 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.js +++ b/src/libs/Navigation/AppNavigator/AuthScreens.js @@ -4,6 +4,7 @@ import PropTypes from 'prop-types'; import _ from 'underscore'; import lodashGet from 'lodash/get'; import {View} from 'react-native'; +import Str from "expensify-common/lib/str"; import withWindowDimensions, {windowDimensionsPropTypes} from '../../../components/withWindowDimensions'; import CONST from '../../../CONST'; import compose from '../../compose'; @@ -35,6 +36,7 @@ import * as SessionUtils from '../../SessionUtils'; import NotFoundPage from '../../../pages/ErrorPage/NotFoundPage'; import getRootNavigatorScreenOptions from './getRootNavigatorScreenOptions'; import DemoSetupPage from '../../../pages/DemoSetupPage'; +import getCurrentUrl from "../currentUrl"; let timezone; let currentAccountID; @@ -137,6 +139,16 @@ class AuthScreens extends React.Component { } componentDidMount() { + const currentUrl = getCurrentUrl(); + const url = new URL(currentUrl); + const isLoggingInAsNewUser = SessionUtils.isLoggingInAsNewUser(currentUrl, this.props.session.email); + // Sign out the current user if we're transitioning with a different user + const isTransitioning = Str.startsWith(url.pathname, Str.normalizeUrl(ROUTES.TRANSITION_BETWEEN_APPS)); + if (isLoggingInAsNewUser && isTransitioning) { + Session.signOutAndRedirectToSignIn(); + return; + } + NetworkConnection.listenForReconnect(); NetworkConnection.onReconnect(() => { if (isLoadingApp) { diff --git a/src/libs/actions/App.js b/src/libs/actions/App.js index b8be35aa1919..43a2a47a374f 100644 --- a/src/libs/actions/App.js +++ b/src/libs/actions/App.js @@ -372,9 +372,6 @@ function setUpPoliciesAndNavigate(session, shouldNavigateToAdminChat) { // Sign out the current user if we're transitioning with a different user const isTransitioning = Str.startsWith(url.pathname, Str.normalizeUrl(ROUTES.TRANSITION_BETWEEN_APPS)); - if (isLoggingInAsNewUser && isTransitioning) { - Session.signOut(); - } const shouldCreateFreePolicy = !isLoggingInAsNewUser && isTransitioning && exitTo === ROUTES.WORKSPACE_NEW; if (shouldCreateFreePolicy) { diff --git a/src/pages/LogOutPreviousUserPage.js b/src/pages/LogOutPreviousUserPage.js index 9055187def16..a8078d920fcb 100644 --- a/src/pages/LogOutPreviousUserPage.js +++ b/src/pages/LogOutPreviousUserPage.js @@ -1,12 +1,10 @@ -import React, {useEffect} from 'react'; -import {Linking} from 'react-native'; +import React, { useEffect } from 'react'; import lodashGet from 'lodash/get'; import PropTypes from 'prop-types'; -import {withOnyx} from 'react-native-onyx'; +import { withOnyx } from 'react-native-onyx'; import ONYXKEYS from '../ONYXKEYS'; import * as Session from '../libs/actions/Session'; import FullScreenLoadingIndicator from '../components/FullscreenLoadingIndicator'; -import * as SessionUtils from '../libs/SessionUtils'; const propTypes = { /** The data about the current session which will be set once the user is authenticated and we return to this component as an AuthScreen */ @@ -25,31 +23,18 @@ const defaultProps = { 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); - } - }); + 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 [], ); - return ; + return ; } LogOutPreviousUserPage.propTypes = propTypes; From e8deb03eec1262fb8c1390249229d6f8885bc9d9 Mon Sep 17 00:00:00 2001 From: Agata Kosior Date: Fri, 6 Oct 2023 10:19:59 +0200 Subject: [PATCH 2/8] fix: apply requested changes --- src/libs/Navigation/AppNavigator/AuthScreens.js | 4 ++-- src/pages/LogOutPreviousUserPage.js | 10 +++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index e0db8905a2b8..b445b0c3ab9c 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.js +++ b/src/libs/Navigation/AppNavigator/AuthScreens.js @@ -4,7 +4,7 @@ import PropTypes from 'prop-types'; import _ from 'underscore'; import lodashGet from 'lodash/get'; import {View} from 'react-native'; -import Str from "expensify-common/lib/str"; +import Str from 'expensify-common/lib/str'; import withWindowDimensions, {windowDimensionsPropTypes} from '../../../components/withWindowDimensions'; import CONST from '../../../CONST'; import compose from '../../compose'; @@ -36,7 +36,7 @@ import * as SessionUtils from '../../SessionUtils'; import NotFoundPage from '../../../pages/ErrorPage/NotFoundPage'; import getRootNavigatorScreenOptions from './getRootNavigatorScreenOptions'; import DemoSetupPage from '../../../pages/DemoSetupPage'; -import getCurrentUrl from "../currentUrl"; +import getCurrentUrl from '../currentUrl'; let timezone; let currentAccountID; diff --git a/src/pages/LogOutPreviousUserPage.js b/src/pages/LogOutPreviousUserPage.js index a8078d920fcb..ccdcce52d964 100644 --- a/src/pages/LogOutPreviousUserPage.js +++ b/src/pages/LogOutPreviousUserPage.js @@ -1,7 +1,7 @@ -import React, { useEffect } from 'react'; +import React, {useEffect} from 'react'; import lodashGet from 'lodash/get'; import PropTypes from 'prop-types'; -import { withOnyx } from 'react-native-onyx'; +import {withOnyx} from 'react-native-onyx'; import ONYXKEYS from '../ONYXKEYS'; import * as Session from '../libs/actions/Session'; import FullScreenLoadingIndicator from '../components/FullscreenLoadingIndicator'; @@ -23,6 +23,10 @@ const defaultProps = { function LogOutPreviousUserPage(props) { useEffect( () => { + // 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', ''); @@ -34,7 +38,7 @@ function LogOutPreviousUserPage(props) { [], ); - return ; + return ; } LogOutPreviousUserPage.propTypes = propTypes; From 4e2d24375a81f77b078f2c9bc6ba2ce910644477 Mon Sep 17 00:00:00 2001 From: Agata Kosior Date: Fri, 6 Oct 2023 12:19:32 +0200 Subject: [PATCH 3/8] fix: fix failing tests --- src/libs/Navigation/AppNavigator/AuthScreens.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index b445b0c3ab9c..b6f6f0c0da1a 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.js +++ b/src/libs/Navigation/AppNavigator/AuthScreens.js @@ -140,10 +140,10 @@ class AuthScreens extends React.Component { componentDidMount() { const currentUrl = getCurrentUrl(); - const url = new URL(currentUrl); + const isLoggingInAsNewUser = SessionUtils.isLoggingInAsNewUser(currentUrl, this.props.session.email); // Sign out the current user if we're transitioning with a different user - const isTransitioning = Str.startsWith(url.pathname, Str.normalizeUrl(ROUTES.TRANSITION_BETWEEN_APPS)); + const isTransitioning = currentUrl.includes(ROUTES.TRANSITION_BETWEEN_APPS) if (isLoggingInAsNewUser && isTransitioning) { Session.signOutAndRedirectToSignIn(); return; @@ -166,7 +166,7 @@ class AuthScreens extends React.Component { User.subscribeToUserEvents(); }); - // If we are on this screen then we are "logged in", but the user might not have "just logged in". They could be reopening the app + // // If we are on this screen then we are "logged in", but the user might not have "just logged in". They could be reopening the app // or returning from background. If so, we'll assume they have some app data already and we can call reconnectApp() instead of openApp(). // Note: If a Guide has enabled the memory only key mode then we do want to run OpenApp as their app will not be rehydrated with // the correct state on refresh. They are explicitly opting out of storing data they would need (i.e. reports_) to take advantage of From b230ba14732148aa92d2658f11d559219e834416 Mon Sep 17 00:00:00 2001 From: Agata Kosior Date: Fri, 6 Oct 2023 12:24:59 +0200 Subject: [PATCH 4/8] fix: minor fix --- src/libs/Navigation/AppNavigator/AuthScreens.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index b6f6f0c0da1a..efadecde0d49 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.js +++ b/src/libs/Navigation/AppNavigator/AuthScreens.js @@ -166,7 +166,7 @@ class AuthScreens extends React.Component { User.subscribeToUserEvents(); }); - // // If we are on this screen then we are "logged in", but the user might not have "just logged in". They could be reopening the app + // If we are on this screen then we are "logged in", but the user might not have "just logged in". They could be reopening the app // or returning from background. If so, we'll assume they have some app data already and we can call reconnectApp() instead of openApp(). // Note: If a Guide has enabled the memory only key mode then we do want to run OpenApp as their app will not be rehydrated with // the correct state on refresh. They are explicitly opting out of storing data they would need (i.e. reports_) to take advantage of From 05fd6fa1e46d10710d9f3173a3234fe80937cd93 Mon Sep 17 00:00:00 2001 From: Agata Kosior Date: Fri, 6 Oct 2023 12:36:29 +0200 Subject: [PATCH 5/8] fix: minor fix --- src/libs/Navigation/AppNavigator/AuthScreens.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index efadecde0d49..ed626817763c 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.js +++ b/src/libs/Navigation/AppNavigator/AuthScreens.js @@ -4,7 +4,6 @@ import PropTypes from 'prop-types'; import _ from 'underscore'; import lodashGet from 'lodash/get'; import {View} from 'react-native'; -import Str from 'expensify-common/lib/str'; import withWindowDimensions, {windowDimensionsPropTypes} from '../../../components/withWindowDimensions'; import CONST from '../../../CONST'; import compose from '../../compose'; From e06a7ba6a5122537625b6a43531dde8c3371cccc Mon Sep 17 00:00:00 2001 From: Agata Kosior Date: Fri, 6 Oct 2023 12:44:38 +0200 Subject: [PATCH 6/8] fix: run prettier --- src/libs/Navigation/AppNavigator/AuthScreens.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index ed626817763c..247b1a91daf9 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.js +++ b/src/libs/Navigation/AppNavigator/AuthScreens.js @@ -142,7 +142,7 @@ class AuthScreens extends React.Component { const isLoggingInAsNewUser = SessionUtils.isLoggingInAsNewUser(currentUrl, this.props.session.email); // Sign out the current user if we're transitioning with a different user - const isTransitioning = currentUrl.includes(ROUTES.TRANSITION_BETWEEN_APPS) + const isTransitioning = currentUrl.includes(ROUTES.TRANSITION_BETWEEN_APPS); if (isLoggingInAsNewUser && isTransitioning) { Session.signOutAndRedirectToSignIn(); return; From 12ecca0689c28e66ca1c2304175b502f2296429a Mon Sep 17 00:00:00 2001 From: Agata Kosior Date: Mon, 16 Oct 2023 21:39:44 +0200 Subject: [PATCH 7/8] fix: restore logging out logic to fix a spinner issue --- src/pages/LogOutPreviousUserPage.js | 31 +++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/pages/LogOutPreviousUserPage.js b/src/pages/LogOutPreviousUserPage.js index ccdcce52d964..9055187def16 100644 --- a/src/pages/LogOutPreviousUserPage.js +++ b/src/pages/LogOutPreviousUserPage.js @@ -1,10 +1,12 @@ import React, {useEffect} from 'react'; +import {Linking} from 'react-native'; import lodashGet from 'lodash/get'; import PropTypes from 'prop-types'; import {withOnyx} from 'react-native-onyx'; import ONYXKEYS from '../ONYXKEYS'; import * as Session from '../libs/actions/Session'; import FullScreenLoadingIndicator from '../components/FullscreenLoadingIndicator'; +import * as SessionUtils from '../libs/SessionUtils'; const propTypes = { /** The data about the current session which will be set once the user is authenticated and we return to this component as an AuthScreen */ @@ -23,16 +25,25 @@ const defaultProps = { function LogOutPreviousUserPage(props) { useEffect( () => { - // 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); - } + 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 [], From 483eb68b62ee73272263fa74826e6d7a67a02ca7 Mon Sep 17 00:00:00 2001 From: Agata Kosior Date: Tue, 17 Oct 2023 15:04:44 +0200 Subject: [PATCH 8/8] fix: apply requested changes --- src/libs/Navigation/AppNavigator/AuthScreens.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index 6f60a526ee30..dd7175dbc6f6 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.js +++ b/src/libs/Navigation/AppNavigator/AuthScreens.js @@ -147,7 +147,6 @@ class AuthScreens extends React.Component { componentDidMount() { const currentUrl = getCurrentUrl(); - const isLoggingInAsNewUser = SessionUtils.isLoggingInAsNewUser(currentUrl, this.props.session.email); // Sign out the current user if we're transitioning with a different user const isTransitioning = currentUrl.includes(ROUTES.TRANSITION_BETWEEN_APPS);