Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix transitioning issue from old dot #28984

Merged
14 changes: 13 additions & 1 deletion src/libs/Navigation/AppNavigator/AuthScreens.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -137,6 +139,16 @@ class AuthScreens extends React.Component {
}

componentDidMount() {
const currentUrl = getCurrentUrl();

const isLoggingInAsNewUser = SessionUtils.isLoggingInAsNewUser(currentUrl, this.props.session.email);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const currentUrl = getCurrentUrl();
const isLoggingInAsNewUser = SessionUtils.isLoggingInAsNewUser(currentUrl, this.props.session.email);
const currentUrl = getCurrentUrl();
const isLoggingInAsNewUser = SessionUtils.isLoggingInAsNewUser(currentUrl, this.props.session.email);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mountiny done 😃

// Sign out the current user if we're transitioning with a different user
const isTransitioning = currentUrl.includes(ROUTES.TRANSITION_BETWEEN_APPS)
if (isLoggingInAsNewUser && isTransitioning) {
Session.signOutAndRedirectToSignIn();
return;
}

NetworkConnection.listenForReconnect();
NetworkConnection.onReconnect(() => {
if (isLoadingApp) {
Expand All @@ -154,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
Expand Down
3 changes: 0 additions & 3 deletions src/libs/actions/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
31 changes: 10 additions & 21 deletions src/pages/LogOutPreviousUserPage.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
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 */
Expand All @@ -25,25 +23,16 @@ 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);
}
});
// 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
[],
Expand Down