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

Refactor ValidateLogin #11457

Merged
merged 11 commits into from
Oct 10, 2022
4 changes: 4 additions & 0 deletions src/Expensify.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import compose from './libs/compose';
import withLocalize, {withLocalizePropTypes} from './components/withLocalize';
import * as User from './libs/actions/User';
import NetworkConnection from './libs/NetworkConnection';
import Navigation from './libs/Navigation/Navigation';

Onyx.registerLogger(({level, message}) => {
if (level === 'alert') {
Expand Down Expand Up @@ -141,6 +142,9 @@ class Expensify extends PureComponent {

setNavigationReady() {
this.setState({isNavigationReady: true});

// Navigate to any pending routes now that the NavigationContainer is ready
Navigation.setIsNavigationReady();
}

/**
Expand Down
23 changes: 22 additions & 1 deletion src/libs/Navigation/Navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const drawerIsReadyPromise = new Promise((resolve) => {
});

let isLoggedIn = false;
let pendingRoute = null;

Onyx.connect({
key: ONYXKEYS.SESSION,
callback: val => isLoggedIn = Boolean(val && val.authToken),
Expand Down Expand Up @@ -130,17 +132,22 @@ function isDrawerRoute(route) {
*/
function navigate(route = ROUTES.HOME) {
if (!canNavigate('navigate', {route})) {
// Store intended route if the navigator is not yet available,
// we will try again after the NavigationContainer is ready
Log.hmmm(`[Navigation] Container not yet ready, storing route as pending: ${route}`);
pendingRoute = route;
return;
}

if (route === ROUTES.HOME) {
if (isLoggedIn) {
if (isLoggedIn && pendingRoute === null) {
openDrawer();
return;
}

// If we're navigating to the signIn page while logged out, pop whatever screen is on top
// since it's guaranteed that the sign in page will be underneath (since it's the initial route).
// Also, if we're coming from a link to validate login (pendingRoute is not null), we want to pop the loading screen.
navigationRef.current.dispatch(StackActions.pop());
return;
}
Expand Down Expand Up @@ -210,6 +217,19 @@ function isActiveRoute(routePath) {
return getActiveRoute().substring(1) === routePath;
}

/**
* Navigate to the route that we originally intended to go to
* but the NavigationContainer was not ready when navigate() was called
*/
function goToPendingRoute() {
if (pendingRoute === null) {
return;
}
Log.hmmm(`[Navigation] Container now ready, going to pending route: ${pendingRoute}`);
navigate(pendingRoute);
pendingRoute = null;
}

/**
* @returns {Promise}
*/
Expand All @@ -218,6 +238,7 @@ function isNavigationReady() {
}

function setIsNavigationReady() {
goToPendingRoute();
resolveNavigationIsReadyPromise();
}

Expand Down
35 changes: 12 additions & 23 deletions src/libs/actions/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@ import * as Link from './Link';
import getSkinToneEmojiFromIndex from '../../components/EmojiPicker/getSkinToneEmojiFromIndex';
import * as SequentialQueue from '../Network/SequentialQueue';
import PusherUtils from '../PusherUtils';
import DateUtils from '../DateUtils';

let sessionAuthToken = '';
let currentUserAccountID = '';
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (val) => {
sessionAuthToken = lodashGet(val, 'authToken', '');
currentUserAccountID = lodashGet(val, 'accountID', '');
},
});
Expand Down Expand Up @@ -208,30 +205,22 @@ function setSecondaryLoginAndNavigate(login, password) {
* @param {String} validateCode
*/
function validateLogin(accountID, validateCode) {
const isLoggedIn = !_.isEmpty(sessionAuthToken);
Onyx.merge(ONYXKEYS.ACCOUNT, {...CONST.DEFAULT_ACCOUNT_DATA, isLoading: true});

DeprecatedAPI.ValidateEmail({
const optimisticData = [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: false,
},
},
];
API.write('ValidateLogin', {
accountID,
validateCode,
}).then((response) => {
if (response.jsonCode === 200) {
const {email} = response;

if (isLoggedIn) {
getUserDetails();
} else {
// Let the user know we've successfully validated their login
const success = lodashGet(response, 'message', `Your secondary login ${email} has been validated.`);
Onyx.merge(ONYXKEYS.ACCOUNT, {success});
}
} else {
Onyx.merge(ONYXKEYS.ACCOUNT, {errors: {[DateUtils.getMicroseconds()]: Localize.translateLocal('resendValidationForm.validationCodeFailedMessage')}});
}
}).finally(() => {
Onyx.merge(ONYXKEYS.ACCOUNT, {isLoading: false});
Navigation.navigate(ROUTES.HOME);
});
}, {optimisticData});
Navigation.navigate(ROUTES.HOME);
}

/**
Expand Down