From eb42af8e300a1b6858bb9fc0000a9ba69582a968 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Tue, 30 Jan 2024 14:37:15 +0100 Subject: [PATCH 01/72] adding new properties --- src/libs/Navigation/types.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libs/Navigation/types.ts b/src/libs/Navigation/types.ts index 2371c764f42a..e4fb13d0b019 100644 --- a/src/libs/Navigation/types.ts +++ b/src/libs/Navigation/types.ts @@ -375,7 +375,9 @@ type PublicScreensParamList = { [SCREENS.HOME]: undefined; [SCREENS.TRANSITION_BETWEEN_APPS]: { email?: string; + accountID?: number; error?: string; + supportAuthToken?: string; shortLivedAuthToken?: string; shortLivedToken?: string; exitTo?: Routes; From fe65f54b51b093e7a96815de81c7ec5910598d32 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Tue, 30 Jan 2024 14:37:43 +0100 Subject: [PATCH 02/72] adding new logic for setting supportal token on public pages --- src/pages/LogInWithShortLivedAuthTokenPage.tsx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/pages/LogInWithShortLivedAuthTokenPage.tsx b/src/pages/LogInWithShortLivedAuthTokenPage.tsx index 811c35fff34e..e4fe674c0394 100644 --- a/src/pages/LogInWithShortLivedAuthTokenPage.tsx +++ b/src/pages/LogInWithShortLivedAuthTokenPage.tsx @@ -31,7 +31,7 @@ function LogInWithShortLivedAuthTokenPage({route, account}: LogInWithShortLivedA const theme = useTheme(); const styles = useThemeStyles(); const {translate} = useLocalize(); - const {email = '', shortLivedAuthToken = '', shortLivedToken = '', exitTo, error} = route?.params ?? {}; + const {email = '', accountID = 0, supportAuthToken = '', shortLivedAuthToken = '', shortLivedToken = '', exitTo, error} = route?.params ?? {}; useEffect(() => { // We have to check for both shortLivedAuthToken and shortLivedToken, as the old mobile app uses shortLivedToken, and is not being actively updated. @@ -44,6 +44,11 @@ function LogInWithShortLivedAuthTokenPage({route, account}: LogInWithShortLivedA return; } + if (!account?.isLoading && accountID && supportAuthToken) { + Session.setSupportAuthToken(supportAuthToken, email, accountID); + return; + } + // If an error is returned as part of the route, ensure we set it in the onyxData for the account if (error) { Session.setAccountError(error); From ad993abe0b3992490e0a59e01a02dd3ccb9d258e Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Tue, 30 Jan 2024 14:42:31 +0100 Subject: [PATCH 03/72] adding logic in case you already have a session --- src/pages/LogOutPreviousUserPage.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/pages/LogOutPreviousUserPage.js b/src/pages/LogOutPreviousUserPage.js index 974823f489ed..757db0fd34fb 100644 --- a/src/pages/LogOutPreviousUserPage.js +++ b/src/pages/LogOutPreviousUserPage.js @@ -57,6 +57,14 @@ function LogOutPreviousUserPage(props) { const shortLivedAuthToken = lodashGet(props, 'route.params.shortLivedAuthToken', ''); Session.signInWithShortLivedAuthToken(email, shortLivedAuthToken); } + + const isSupportalLogin = lodashGet(props, 'route.params.supportAuthToken', '') !== ''; + if (isSupportalLogin) { + const email = lodashGet(props, 'route.params.email', ''); + const supportAuthToken = lodashGet(props, 'route.params.supportAuthToken', ''); + const accountID = lodashGet(props, 'route.params.accountID', 0); + Session.setSupportAuthToken(supportAuthToken, email, accountID); + } }); // We only want to run this effect once on mount (when the page first loads after transitioning from OldDot) From f5acfa81cb850c0c94cd9555e6d40aebe549ef31 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Tue, 30 Jan 2024 15:05:03 +0100 Subject: [PATCH 04/72] converting accountID to Number --- src/pages/LogInWithShortLivedAuthTokenPage.tsx | 2 +- src/pages/LogOutPreviousUserPage.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pages/LogInWithShortLivedAuthTokenPage.tsx b/src/pages/LogInWithShortLivedAuthTokenPage.tsx index e4fe674c0394..20b9a09218fb 100644 --- a/src/pages/LogInWithShortLivedAuthTokenPage.tsx +++ b/src/pages/LogInWithShortLivedAuthTokenPage.tsx @@ -45,7 +45,7 @@ function LogInWithShortLivedAuthTokenPage({route, account}: LogInWithShortLivedA } if (!account?.isLoading && accountID && supportAuthToken) { - Session.setSupportAuthToken(supportAuthToken, email, accountID); + Session.setSupportAuthToken(supportAuthToken, email, Number(accountID)); return; } diff --git a/src/pages/LogOutPreviousUserPage.js b/src/pages/LogOutPreviousUserPage.js index 757db0fd34fb..e64f569946b0 100644 --- a/src/pages/LogOutPreviousUserPage.js +++ b/src/pages/LogOutPreviousUserPage.js @@ -63,7 +63,7 @@ function LogOutPreviousUserPage(props) { const email = lodashGet(props, 'route.params.email', ''); const supportAuthToken = lodashGet(props, 'route.params.supportAuthToken', ''); const accountID = lodashGet(props, 'route.params.accountID', 0); - Session.setSupportAuthToken(supportAuthToken, email, accountID); + Session.setSupportAuthToken(supportAuthToken, email, Number(accountID)); } }); From 9020de746908b1388cf2376797231f494db28cd6 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Tue, 30 Jan 2024 15:55:07 +0100 Subject: [PATCH 05/72] adding type of auth token to setSupportAuthToken --- src/libs/actions/Session/index.ts | 33 ++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 4fbeba0abaa6..5ec4bb6a9531 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -85,21 +85,31 @@ Onyx.connect({ */ function signOut() { Log.info('Flushing logs before signing out', true, {}, true); + + // In case this is a supportal token, we won't have infinite sessions setup since the token will be + // short lived and we can just skip calling logout. + if (!isSupportalToken()) { + const params: LogOutParams = { + // Send current authToken because we will immediately clear it once triggering this command + authToken: NetworkStore.getAuthToken(), + partnerUserID: credentials?.autoGeneratedLogin ?? '', + partnerName: CONFIG.EXPENSIFY.PARTNER_NAME, + partnerPassword: CONFIG.EXPENSIFY.PARTNER_PASSWORD, + shouldRetry: false, + }; + + API.write(WRITE_COMMANDS.LOG_OUT, params); + } - const params: LogOutParams = { - // Send current authToken because we will immediately clear it once triggering this command - authToken: NetworkStore.getAuthToken(), - partnerUserID: credentials?.autoGeneratedLogin ?? '', - partnerName: CONFIG.EXPENSIFY.PARTNER_NAME, - partnerPassword: CONFIG.EXPENSIFY.PARTNER_PASSWORD, - shouldRetry: false, - }; - - API.write(WRITE_COMMANDS.LOG_OUT, params); clearCache().then(() => { Log.info('Cleared all cache data', true, {}, true); }); Timing.clearData(); + +} + +function isSupportalToken(): boolean { + return sessionAuthTokenType === 'supportal'; } /** @@ -540,11 +550,12 @@ function invalidateAuthToken() { function setSupportAuthToken(supportAuthToken: string, email: string, accountID: number) { if (supportAuthToken) { Onyx.merge(ONYXKEYS.SESSION, { + authTokenType: 'supportal', authToken: '1', supportAuthToken, email, accountID, - }); + }).then(() => Log.info("[Supportal] Auth token set")); } else { Onyx.set(ONYXKEYS.SESSION, {}); } From 28c567fa0d90e224c15ca5f66bb6a30ee8b518bb Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Tue, 6 Feb 2024 17:35:11 +0100 Subject: [PATCH 06/72] adding logs --- src/libs/actions/Session/index.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 5ec4bb6a9531..2cbf0090eac3 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -87,8 +87,10 @@ function signOut() { Log.info('Flushing logs before signing out', true, {}, true); // In case this is a supportal token, we won't have infinite sessions setup since the token will be - // short lived and we can just skip calling logout. - if (!isSupportalToken()) { + // short lived. So we can just remove the token and we can just skip calling logout. + if (isSupportalToken()) { + setSupportAuthToken(""); + } else { const params: LogOutParams = { // Send current authToken because we will immediately clear it once triggering this command authToken: NetworkStore.getAuthToken(), @@ -547,18 +549,23 @@ function invalidateAuthToken() { /** * Sets the SupportToken */ -function setSupportAuthToken(supportAuthToken: string, email: string, accountID: number) { +function setSupportAuthToken(supportAuthToken: string, email?: string, accountID?: number) { if (supportAuthToken) { Onyx.merge(ONYXKEYS.SESSION, { authTokenType: 'supportal', - authToken: '1', + authToken: 'dummy-token-supportal-will-be-used', supportAuthToken, email, accountID, - }).then(() => Log.info("[Supportal] Auth token set")); + }).then(() => { + Log.info("[Supportal] Authtoken set"); + }); } else { - Onyx.set(ONYXKEYS.SESSION, {}); + Onyx.set(ONYXKEYS.SESSION, {}).then(()=> { + Log.info("[Supportal] Authtoken removed"); + }); } + Onyx.set(ONYXKEYS.LAST_VISITED_PATH, ''); NetworkStore.setSupportAuthToken(supportAuthToken); } From 0b896fdcc98fadcd0891db8e2ad82515f8761b92 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Tue, 6 Feb 2024 17:59:58 +0100 Subject: [PATCH 07/72] fixing linter --- src/libs/actions/Session/index.ts | 54 +++++++++++++++---------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 2cbf0090eac3..58032761774a 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -80,6 +80,33 @@ Onyx.connect({ callback: (val) => (preferredLocale = val), }); +function isSupportalToken(): boolean { + return sessionAuthTokenType === 'supportal'; +} + +/** + * Sets the SupportToken + */ +function setSupportAuthToken(supportAuthToken: string, email?: string, accountID?: number) { + if (supportAuthToken) { + Onyx.merge(ONYXKEYS.SESSION, { + authTokenType: 'supportal', + authToken: 'dummy-token-supportal-will-be-used', + supportAuthToken, + email, + accountID, + }).then(() => { + Log.info("[Supportal] Authtoken set"); + }); + } else { + Onyx.set(ONYXKEYS.SESSION, {}).then(()=> { + Log.info("[Supportal] Authtoken removed"); + }); + } + Onyx.set(ONYXKEYS.LAST_VISITED_PATH, ''); + NetworkStore.setSupportAuthToken(supportAuthToken); +} + /** * Clears the Onyx store and redirects user to the sign in page */ @@ -110,10 +137,6 @@ function signOut() { } -function isSupportalToken(): boolean { - return sessionAuthTokenType === 'supportal'; -} - /** * Checks if the account is an anonymous account. */ @@ -546,29 +569,6 @@ function invalidateAuthToken() { Onyx.merge(ONYXKEYS.SESSION, {authToken: 'pizza'}); } -/** - * Sets the SupportToken - */ -function setSupportAuthToken(supportAuthToken: string, email?: string, accountID?: number) { - if (supportAuthToken) { - Onyx.merge(ONYXKEYS.SESSION, { - authTokenType: 'supportal', - authToken: 'dummy-token-supportal-will-be-used', - supportAuthToken, - email, - accountID, - }).then(() => { - Log.info("[Supportal] Authtoken set"); - }); - } else { - Onyx.set(ONYXKEYS.SESSION, {}).then(()=> { - Log.info("[Supportal] Authtoken removed"); - }); - } - Onyx.set(ONYXKEYS.LAST_VISITED_PATH, ''); - NetworkStore.setSupportAuthToken(supportAuthToken); -} - /** * Clear the credentials and partial sign in session so the user can taken back to first Login step */ From 9b015a2fd7bd75da63525a0297d66b6deaf93a8b Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Tue, 6 Feb 2024 18:49:14 +0100 Subject: [PATCH 08/72] prettier --- src/libs/actions/Session/index.ts | 13 ++++++------- src/pages/LogInWithShortLivedAuthTokenPage.tsx | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 58032761774a..4225b41bb703 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -96,11 +96,11 @@ function setSupportAuthToken(supportAuthToken: string, email?: string, accountID email, accountID, }).then(() => { - Log.info("[Supportal] Authtoken set"); + Log.info('[Supportal] Authtoken set'); }); } else { - Onyx.set(ONYXKEYS.SESSION, {}).then(()=> { - Log.info("[Supportal] Authtoken removed"); + Onyx.set(ONYXKEYS.SESSION, {}).then(() => { + Log.info('[Supportal] Authtoken removed'); }); } Onyx.set(ONYXKEYS.LAST_VISITED_PATH, ''); @@ -112,11 +112,11 @@ function setSupportAuthToken(supportAuthToken: string, email?: string, accountID */ function signOut() { Log.info('Flushing logs before signing out', true, {}, true); - + // In case this is a supportal token, we won't have infinite sessions setup since the token will be // short lived. So we can just remove the token and we can just skip calling logout. if (isSupportalToken()) { - setSupportAuthToken(""); + setSupportAuthToken(''); } else { const params: LogOutParams = { // Send current authToken because we will immediately clear it once triggering this command @@ -126,7 +126,7 @@ function signOut() { partnerPassword: CONFIG.EXPENSIFY.PARTNER_PASSWORD, shouldRetry: false, }; - + API.write(WRITE_COMMANDS.LOG_OUT, params); } @@ -134,7 +134,6 @@ function signOut() { Log.info('Cleared all cache data', true, {}, true); }); Timing.clearData(); - } /** diff --git a/src/pages/LogInWithShortLivedAuthTokenPage.tsx b/src/pages/LogInWithShortLivedAuthTokenPage.tsx index 2c8d3cf546fb..afd6a0e0a8b7 100644 --- a/src/pages/LogInWithShortLivedAuthTokenPage.tsx +++ b/src/pages/LogInWithShortLivedAuthTokenPage.tsx @@ -31,7 +31,7 @@ function LogInWithShortLivedAuthTokenPage({route, account}: LogInWithShortLivedA const theme = useTheme(); const styles = useThemeStyles(); const {translate} = useLocalize(); - const {email = '', accountID = 0, supportAuthToken = '', shortLivedAuthToken = '', shortLivedToken = '', exitTo, error} = route?.params ?? {}; + const {email = '', accountID = 0, supportAuthToken = '', shortLivedAuthToken = '', shortLivedToken = '', exitTo, error} = route?.params ?? {}; useEffect(() => { // We have to check for both shortLivedAuthToken and shortLivedToken, as the old mobile app uses shortLivedToken, and is not being actively updated. From fde063a28ba096e498ef50d6633d5cbbec7db906 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 19 Feb 2024 16:48:06 +0100 Subject: [PATCH 09/72] fixes after merge --- src/libs/Navigation/types.ts | 2 ++ src/pages/LogOutPreviousUserPage.tsx | 3 +++ 2 files changed, 5 insertions(+) diff --git a/src/libs/Navigation/types.ts b/src/libs/Navigation/types.ts index 06a4b65a8785..5b7252a9ec71 100644 --- a/src/libs/Navigation/types.ts +++ b/src/libs/Navigation/types.ts @@ -496,6 +496,8 @@ type AuthScreensParamList = { shouldForceLogin: string; email: string; shortLivedAuthToken: string; + supportAuthToken: string; + accountID: number; exitTo: string; }; [SCREENS.CONCIERGE]: undefined; diff --git a/src/pages/LogOutPreviousUserPage.tsx b/src/pages/LogOutPreviousUserPage.tsx index f68344604dfa..8bb5dba70ecd 100644 --- a/src/pages/LogOutPreviousUserPage.tsx +++ b/src/pages/LogOutPreviousUserPage.tsx @@ -31,6 +31,9 @@ function LogOutPreviousUserPage({session, route}: LogOutPreviousUserPageProps) { if (isLoggingInAsNewUser) { SessionActions.signOutAndRedirectToSignIn(); } + if (route.params.supportAuthToken !== "") { + SessionActions.setSupportAuthToken(route.params.supportAuthToken, route.params.email, Number(route.params.accountID)); + } // 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. From 807a030f9ffed7f5c7ca8c36a9046f2012ca9740 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 19 Feb 2024 16:50:14 +0100 Subject: [PATCH 10/72] remove unused file --- src/pages/LogOutPreviousUserPage.js | 104 ---------------------------- 1 file changed, 104 deletions(-) delete mode 100644 src/pages/LogOutPreviousUserPage.js diff --git a/src/pages/LogOutPreviousUserPage.js b/src/pages/LogOutPreviousUserPage.js deleted file mode 100644 index 5bfa03250501..000000000000 --- a/src/pages/LogOutPreviousUserPage.js +++ /dev/null @@ -1,104 +0,0 @@ -import lodashGet from 'lodash/get'; -import PropTypes from 'prop-types'; -import React, {useContext, useEffect} from 'react'; -import {Linking, NativeModules} from 'react-native'; -import {withOnyx} from 'react-native-onyx'; -import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator'; -import InitialUrlContext from '@libs/InitialUrlContext'; -import Log from '@libs/Log'; -import Navigation from '@libs/Navigation/Navigation'; -import * as SessionUtils from '@libs/SessionUtils'; -import * as Session from '@userActions/Session'; -import CONST from '@src/CONST'; -import ONYXKEYS from '@src/ONYXKEYS'; -import ROUTES from '@src/ROUTES'; - -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 */ - email: PropTypes.string, - }), -}; - -const defaultProps = { - account: { - isLoading: false, - }, - session: { - email: null, - }, -}; - -// This page is responsible for handling transitions from OldDot. Specifically, it logs the current user -// out if the transition is for another user. -// -// This component should not do any other navigation as that handled in App.setUpPoliciesAndNavigate -function LogOutPreviousUserPage(props) { - const initUrl = useContext(InitialUrlContext); - useEffect(() => { - Linking.getInitialURL().then((url) => { - const sessionEmail = props.session.email; - const transitionUrl = NativeModules.HybridAppModule ? CONST.DEEPLINK_BASE_URL + initUrl : url; - 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) { - Log.info('LogOutPreviousUserPage - forcing login with shortLivedAuthToken'); - const email = lodashGet(props, 'route.params.email', ''); - const shortLivedAuthToken = lodashGet(props, 'route.params.shortLivedAuthToken', ''); - Session.signInWithShortLivedAuthToken(email, shortLivedAuthToken); - } - - const isSupportalLogin = lodashGet(props, 'route.params.supportAuthToken', '') !== ''; - if (isSupportalLogin) { - const email = lodashGet(props, 'route.params.email', ''); - const supportAuthToken = lodashGet(props, 'route.params.supportAuthToken', ''); - const accountID = lodashGet(props, 'route.params.accountID', 0); - Session.setSupportAuthToken(supportAuthToken, email, Number(accountID)); - } - - const exitTo = lodashGet(props, 'route.params.exitTo', ''); - // We don't want to navigate to the exitTo route when creating a new workspace from a deep link, - // because we already handle creating the optimistic policy and navigating to it in App.setUpPoliciesAndNavigate, - // which is already called when AuthScreens mounts. - if (exitTo && exitTo !== ROUTES.WORKSPACE_NEW && !props.account.isLoading && !isLoggingInAsNewUser) { - Navigation.isNavigationReady().then(() => { - // remove this screen and navigate to exit route - const exitUrl = NativeModules.HybridAppModule ? Navigation.parseHybridAppUrl(exitTo) : exitTo; - Navigation.goBack(); - Navigation.navigate(exitUrl); - }); - } - }); - }, [initUrl, props]); - - return ; -} - -LogOutPreviousUserPage.propTypes = propTypes; -LogOutPreviousUserPage.defaultProps = defaultProps; -LogOutPreviousUserPage.displayName = 'LogOutPreviousUserPage'; - -export default withOnyx({ - account: { - key: ONYXKEYS.ACCOUNT, - }, - session: { - key: ONYXKEYS.SESSION, - }, -})(LogOutPreviousUserPage); From 39c18b1001853d49d39d0b55d44e7144e9395538 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 19 Feb 2024 17:04:23 +0100 Subject: [PATCH 11/72] removing function setSupportAuthToken from network since it is already watching the data from onyx --- src/libs/Network/NetworkStore.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/libs/Network/NetworkStore.ts b/src/libs/Network/NetworkStore.ts index 5b93c9adc11a..149ae3c92824 100644 --- a/src/libs/Network/NetworkStore.ts +++ b/src/libs/Network/NetworkStore.ts @@ -103,10 +103,6 @@ function getSupportAuthToken(): string | null { return supportAuthToken; } -function setSupportAuthToken(newSupportAuthToken: string) { - supportAuthToken = newSupportAuthToken; -} - function setAuthToken(newAuthToken: string | null) { authToken = newAuthToken; } @@ -140,6 +136,5 @@ export { getCredentials, checkRequiredData, getSupportAuthToken, - setSupportAuthToken, isSupportRequest, }; From 0a7995578e03e9e4abb25b1eaffccc6d47306483 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 19 Feb 2024 17:17:35 +0100 Subject: [PATCH 12/72] creating new parameters for using on API request --- src/libs/API/parameters/SignInWithSupportAuthTokenParams.ts | 5 +++++ src/libs/API/parameters/index.ts | 1 + src/libs/API/types.ts | 2 ++ 3 files changed, 8 insertions(+) create mode 100644 src/libs/API/parameters/SignInWithSupportAuthTokenParams.ts diff --git a/src/libs/API/parameters/SignInWithSupportAuthTokenParams.ts b/src/libs/API/parameters/SignInWithSupportAuthTokenParams.ts new file mode 100644 index 000000000000..ad205fce236e --- /dev/null +++ b/src/libs/API/parameters/SignInWithSupportAuthTokenParams.ts @@ -0,0 +1,5 @@ +type SignInWithSupportAuthTokenParams = { + authToken: string; +}; + +export default SignInWithSupportAuthTokenParams; diff --git a/src/libs/API/parameters/index.ts b/src/libs/API/parameters/index.ts index b9042dcc1215..6e83c416f71e 100644 --- a/src/libs/API/parameters/index.ts +++ b/src/libs/API/parameters/index.ts @@ -52,6 +52,7 @@ export type {default as SendPerformanceTimingParams} from './SendPerformanceTimi export type {default as SetContactMethodAsDefaultParams} from './SetContactMethodAsDefaultParams'; export type {default as SignInUserWithLinkParams} from './SignInUserWithLinkParams'; export type {default as SignInWithShortLivedAuthTokenParams} from './SignInWithShortLivedAuthTokenParams'; +export type {default as SignInWithSupportAuthTokenParams} from './SignInWithSupportAuthTokenParams'; export type {default as UnlinkLoginParams} from './UnlinkLoginParams'; export type {default as UpdateAutomaticTimezoneParams} from './UpdateAutomaticTimezoneParams'; export type {default as UpdateChatPriorityModeParams} from './UpdateChatPriorityModeParams'; diff --git a/src/libs/API/types.ts b/src/libs/API/types.ts index 7c7695ebef57..531ffc220354 100644 --- a/src/libs/API/types.ts +++ b/src/libs/API/types.ts @@ -317,6 +317,7 @@ const READ_COMMANDS = { OPEN_ENABLE_PAYMENTS_PAGE: 'OpenEnablePaymentsPage', BEGIN_SIGNIN: 'BeginSignIn', SIGN_IN_WITH_SHORT_LIVED_AUTH_TOKEN: 'SignInWithShortLivedAuthToken', + SIGN_IN_WITH_SUPPORT_AUTH_TOKEN: 'SignInWithSupportAuthToken', OPEN_WORKSPACE_REIMBURSE_VIEW: 'OpenWorkspaceReimburseView', OPEN_WORKSPACE: 'OpenWorkspace', OPEN_WORKSPACE_MEMBERS_PAGE: 'OpenWorkspaceMembersPage', @@ -351,6 +352,7 @@ type ReadCommandParameters = { [READ_COMMANDS.OPEN_ENABLE_PAYMENTS_PAGE]: EmptyObject; [READ_COMMANDS.BEGIN_SIGNIN]: Parameters.BeginSignInParams; [READ_COMMANDS.SIGN_IN_WITH_SHORT_LIVED_AUTH_TOKEN]: Parameters.SignInWithShortLivedAuthTokenParams; + [READ_COMMANDS.SIGN_IN_WITH_SUPPORT_AUTH_TOKEN]: Parameters.SignInWithSupportAuthTokenParams; [READ_COMMANDS.OPEN_WORKSPACE_REIMBURSE_VIEW]: Parameters.OpenWorkspaceReimburseViewParams; [READ_COMMANDS.OPEN_WORKSPACE]: Parameters.OpenWorkspaceParams; [READ_COMMANDS.OPEN_WORKSPACE_MEMBERS_PAGE]: Parameters.OpenWorkspaceMembersPageParams; From 594693cd1623b75023499a92955337e2eeb7b503 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 19 Feb 2024 17:17:51 +0100 Subject: [PATCH 13/72] adding new method to call API with support token --- src/libs/actions/Session/index.ts | 54 ++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 261c43bbea1b..3513ae1314c3 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -18,6 +18,7 @@ import type { RequestUnlinkValidationLinkParams, SignInUserWithLinkParams, SignInWithShortLivedAuthTokenParams, + SignInWithSupportAuthTokenParams, UnlinkLoginParams, ValidateTwoFactorAuthParams, } from '@libs/API/parameters'; @@ -105,7 +106,58 @@ function setSupportAuthToken(supportAuthToken: string, email?: string, accountID }); } Onyx.set(ONYXKEYS.LAST_VISITED_PATH, ''); - NetworkStore.setSupportAuthToken(supportAuthToken); +} + + +function getShortLivedLoginParams() { + const optimisticData: OnyxUpdate[] = [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.ACCOUNT, + value: { + ...CONST.DEFAULT_ACCOUNT_DATA, + isLoading: true, + }, + }, + // We are making a temporary modification to 'signedInWithShortLivedAuthToken' to ensure that 'App.openApp' will be called at least once + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.SESSION, + value: { + signedInWithShortLivedAuthToken: true, + }, + }, + ]; + + // Subsequently, we revert it back to the default value of 'signedInWithShortLivedAuthToken' in 'successData' or 'failureData' to ensure the user is logged out on refresh + // We are combining both success and failure data params into one const as they are identical + const resolutionData: OnyxUpdate[] = [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.ACCOUNT, + value: { + isLoading: false, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.SESSION, + value: { + signedInWithShortLivedAuthToken: null, + }, + }, + ]; + + return {optimisticData, resolutionData}; +} + +/** + * This method should be used when we are being redirected from oldDot to NewDot on a supportal request + */ +function signInWithSupportAuthToken(authToken: string) { + const { optimisticData, resolutionData } = getShortLivedLoginParams(); + const params: SignInWithSupportAuthTokenParams = {authToken}; + API.read(READ_COMMANDS.SIGN_IN_WITH_SUPPORT_AUTH_TOKEN, params, {optimisticData, successData: resolutionData, failureData: resolutionData}); } /** From a453d37fcece3112ce94d8e1a147035391b0348b Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 19 Feb 2024 17:18:59 +0100 Subject: [PATCH 14/72] reusing function on signInWithShortLivedAuthToken --- src/libs/actions/Session/index.ts | 41 ++----------------------------- 1 file changed, 2 insertions(+), 39 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 3513ae1314c3..0650cccaeb62 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -108,7 +108,6 @@ function setSupportAuthToken(supportAuthToken: string, email?: string, accountID Onyx.set(ONYXKEYS.LAST_VISITED_PATH, ''); } - function getShortLivedLoginParams() { const optimisticData: OnyxUpdate[] = [ { @@ -400,44 +399,8 @@ function beginGoogleSignIn(token: string | null) { * re-authenticating after an authToken expires. */ function signInWithShortLivedAuthToken(email: string, authToken: string) { - const optimisticData: OnyxUpdate[] = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.ACCOUNT, - value: { - ...CONST.DEFAULT_ACCOUNT_DATA, - isLoading: true, - }, - }, - // We are making a temporary modification to 'signedInWithShortLivedAuthToken' to ensure that 'App.openApp' will be called at least once - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.SESSION, - value: { - signedInWithShortLivedAuthToken: true, - }, - }, - ]; - - // Subsequently, we revert it back to the default value of 'signedInWithShortLivedAuthToken' in 'successData' or 'failureData' to ensure the user is logged out on refresh - // We are combining both success and failure data params into one const as they are identical - const resolutionData: OnyxUpdate[] = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.ACCOUNT, - value: { - isLoading: false, - }, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.SESSION, - value: { - signedInWithShortLivedAuthToken: null, - }, - }, - ]; - + const { optimisticData, resolutionData } = getShortLivedLoginParams(); + const successData = resolutionData; const failureData = resolutionData; From 234df268755e594b1a06945897fde4dfcf1329ed Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 19 Feb 2024 17:42:44 +0100 Subject: [PATCH 15/72] prettier --- src/libs/actions/Session/index.ts | 6 +++--- src/pages/LogOutPreviousUserPage.tsx | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 0650cccaeb62..7a34f0fd14e5 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -154,7 +154,7 @@ function getShortLivedLoginParams() { * This method should be used when we are being redirected from oldDot to NewDot on a supportal request */ function signInWithSupportAuthToken(authToken: string) { - const { optimisticData, resolutionData } = getShortLivedLoginParams(); + const {optimisticData, resolutionData} = getShortLivedLoginParams(); const params: SignInWithSupportAuthTokenParams = {authToken}; API.read(READ_COMMANDS.SIGN_IN_WITH_SUPPORT_AUTH_TOKEN, params, {optimisticData, successData: resolutionData, failureData: resolutionData}); } @@ -399,8 +399,8 @@ function beginGoogleSignIn(token: string | null) { * re-authenticating after an authToken expires. */ function signInWithShortLivedAuthToken(email: string, authToken: string) { - const { optimisticData, resolutionData } = getShortLivedLoginParams(); - + const {optimisticData, resolutionData} = getShortLivedLoginParams(); + const successData = resolutionData; const failureData = resolutionData; diff --git a/src/pages/LogOutPreviousUserPage.tsx b/src/pages/LogOutPreviousUserPage.tsx index 8bb5dba70ecd..143584da7bd3 100644 --- a/src/pages/LogOutPreviousUserPage.tsx +++ b/src/pages/LogOutPreviousUserPage.tsx @@ -31,7 +31,7 @@ function LogOutPreviousUserPage({session, route}: LogOutPreviousUserPageProps) { if (isLoggingInAsNewUser) { SessionActions.signOutAndRedirectToSignIn(); } - if (route.params.supportAuthToken !== "") { + if (route.params.supportAuthToken !== '') { SessionActions.setSupportAuthToken(route.params.supportAuthToken, route.params.email, Number(route.params.accountID)); } From fccc71e41e2987fec67537f250ce836a8844edc7 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 19 Feb 2024 17:48:36 +0100 Subject: [PATCH 16/72] calling right method now --- src/libs/actions/Session/index.ts | 1 + src/pages/LogOutPreviousUserPage.tsx | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 7a34f0fd14e5..34deb0413ddf 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -961,4 +961,5 @@ export { validateTwoFactorAuth, waitForUserSignIn, canAccessRouteByAnonymousUser, + signInWithSupportAuthToken, }; diff --git a/src/pages/LogOutPreviousUserPage.tsx b/src/pages/LogOutPreviousUserPage.tsx index 143584da7bd3..9c5049f96b8f 100644 --- a/src/pages/LogOutPreviousUserPage.tsx +++ b/src/pages/LogOutPreviousUserPage.tsx @@ -31,8 +31,10 @@ function LogOutPreviousUserPage({session, route}: LogOutPreviousUserPageProps) { if (isLoggingInAsNewUser) { SessionActions.signOutAndRedirectToSignIn(); } - if (route.params.supportAuthToken !== '') { - SessionActions.setSupportAuthToken(route.params.supportAuthToken, route.params.email, Number(route.params.accountID)); + + if (route.params.supportAuthToken !== "") { + SessionActions.signInWithSupportAuthToken(route.params.supportAuthToken); + return; } // We need to signin and fetch a new authToken, if a user was already authenticated in NewDot, and was redirected to OldDot From e8401c1ab763b2206ed448aa36e1a7bc042c131a Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 19 Feb 2024 17:50:02 +0100 Subject: [PATCH 17/72] changing to right method on login page --- src/pages/LogInWithShortLivedAuthTokenPage.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pages/LogInWithShortLivedAuthTokenPage.tsx b/src/pages/LogInWithShortLivedAuthTokenPage.tsx index afd6a0e0a8b7..7631a93eaba2 100644 --- a/src/pages/LogInWithShortLivedAuthTokenPage.tsx +++ b/src/pages/LogInWithShortLivedAuthTokenPage.tsx @@ -31,7 +31,7 @@ function LogInWithShortLivedAuthTokenPage({route, account}: LogInWithShortLivedA const theme = useTheme(); const styles = useThemeStyles(); const {translate} = useLocalize(); - const {email = '', accountID = 0, supportAuthToken = '', shortLivedAuthToken = '', shortLivedToken = '', exitTo, error} = route?.params ?? {}; + const {email = '', supportAuthToken = '', shortLivedAuthToken = '', shortLivedToken = '', exitTo, error} = route?.params ?? {}; useEffect(() => { // We have to check for both shortLivedAuthToken and shortLivedToken, as the old mobile app uses shortLivedToken, and is not being actively updated. @@ -44,8 +44,8 @@ function LogInWithShortLivedAuthTokenPage({route, account}: LogInWithShortLivedA return; } - if (!account?.isLoading && accountID && supportAuthToken) { - Session.setSupportAuthToken(supportAuthToken, email, Number(accountID)); + if (!account?.isLoading && supportAuthToken) { + Session.signInWithSupportAuthToken(supportAuthToken); return; } From 51a84d3d0070f622ab82b72071d45effe443769c Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Tue, 20 Feb 2024 16:15:30 +0100 Subject: [PATCH 18/72] prettier --- src/pages/LogOutPreviousUserPage.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/LogOutPreviousUserPage.tsx b/src/pages/LogOutPreviousUserPage.tsx index 9c5049f96b8f..d69286232072 100644 --- a/src/pages/LogOutPreviousUserPage.tsx +++ b/src/pages/LogOutPreviousUserPage.tsx @@ -32,7 +32,7 @@ function LogOutPreviousUserPage({session, route}: LogOutPreviousUserPageProps) { SessionActions.signOutAndRedirectToSignIn(); } - if (route.params.supportAuthToken !== "") { + if (route.params.supportAuthToken !== '') { SessionActions.signInWithSupportAuthToken(route.params.supportAuthToken); return; } From fe97f398070e1539317c7ef2b44a8076a410f9d6 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Tue, 20 Feb 2024 17:45:21 +0100 Subject: [PATCH 19/72] changing method from getSupportAuthToken to isSupportAuthToken --- src/libs/Network/NetworkStore.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libs/Network/NetworkStore.ts b/src/libs/Network/NetworkStore.ts index 149ae3c92824..5b1ad0eac89b 100644 --- a/src/libs/Network/NetworkStore.ts +++ b/src/libs/Network/NetworkStore.ts @@ -5,7 +5,7 @@ import type Credentials from '@src/types/onyx/Credentials'; let credentials: Credentials | null = null; let authToken: string | null = null; -let supportAuthToken: string | null = null; +let authTokenType: string | null = null; let currentUserEmail: string | null = null; let offline = false; let authenticating = false; @@ -51,7 +51,7 @@ Onyx.connect({ key: ONYXKEYS.SESSION, callback: (val) => { authToken = val?.authToken ?? null; - supportAuthToken = val?.supportAuthToken ?? null; + authTokenType = val?.authTokenType ?? null; currentUserEmail = val?.email ?? null; checkRequiredData(); }, @@ -99,8 +99,8 @@ function isSupportRequest(command: string): boolean { return [READ_COMMANDS.OPEN_APP, SIDE_EFFECT_REQUEST_COMMANDS.RECONNECT_APP, SIDE_EFFECT_REQUEST_COMMANDS.OPEN_REPORT].some((cmd) => cmd === command); } -function getSupportAuthToken(): string | null { - return supportAuthToken; +function isSupportAuthToken(): boolean { + return authTokenType === 'support'; } function setAuthToken(newAuthToken: string | null) { @@ -135,6 +135,6 @@ export { setIsAuthenticating, getCredentials, checkRequiredData, - getSupportAuthToken, + isSupportAuthToken, isSupportRequest, }; From f9b4116ea4727d425c22657dda37f5e2bcaad82d Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Tue, 20 Feb 2024 17:47:34 +0100 Subject: [PATCH 20/72] simplifying token check --- src/libs/Network/enhanceParameters.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/libs/Network/enhanceParameters.ts b/src/libs/Network/enhanceParameters.ts index e14acda5ff56..1b41dc702868 100644 --- a/src/libs/Network/enhanceParameters.ts +++ b/src/libs/Network/enhanceParameters.ts @@ -16,12 +16,11 @@ function isAuthTokenRequired(command: string): boolean { export default function enhanceParameters(command: string, parameters: Record): Record { const finalParameters = {...parameters}; - if (isAuthTokenRequired(command)) { - if (NetworkStore.getSupportAuthToken() && NetworkStore.isSupportRequest(command)) { - finalParameters.authToken = NetworkStore.getSupportAuthToken(); - } else if (!parameters.authToken) { - finalParameters.authToken = NetworkStore.getAuthToken(); - } + if (isAuthTokenRequired(command) + && (NetworkStore.isSupportAuthToken() + && NetworkStore.isSupportRequest(command) + || !parameters.authToken)) { + finalParameters.authToken = NetworkStore.getAuthToken(); } finalParameters.referer = CONFIG.EXPENSIFY.EXPENSIFY_CASH_REFERER; From eaa918605c050abde5b46e188b084a20008fdddc Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Tue, 20 Feb 2024 17:48:05 +0100 Subject: [PATCH 21/72] checking new method for support auth token --- src/libs/Request.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/Request.ts b/src/libs/Request.ts index aa94eccbca2e..ef9a2b70719f 100644 --- a/src/libs/Request.ts +++ b/src/libs/Request.ts @@ -12,7 +12,7 @@ function makeXHR(request: Request): Promise { return NetworkStore.hasReadRequiredDataFromStorage().then((): Promise => { // If we're using the Supportal token and this is not a Supportal request // let's just return a promise that will resolve itself. - if (NetworkStore.getSupportAuthToken() && !NetworkStore.isSupportRequest(request.command)) { + if (NetworkStore.isSupportAuthToken() && !NetworkStore.isSupportRequest(request.command)) { return new Promise((resolve) => resolve()); } From 25032f139fc30f20f71f1db727740df87b68501c Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Tue, 20 Feb 2024 17:50:22 +0100 Subject: [PATCH 22/72] updating comment --- src/libs/actions/Session/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 34deb0413ddf..9a88013e93c4 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -87,14 +87,14 @@ function isSupportalToken(): boolean { } /** - * Sets the SupportToken + * Sets the SupportToken. This method will only be used on SignOut when an user + * is using a token of type support or on dev. */ function setSupportAuthToken(supportAuthToken: string, email?: string, accountID?: number) { if (supportAuthToken) { Onyx.merge(ONYXKEYS.SESSION, { - authTokenType: 'supportal', - authToken: 'dummy-token-supportal-will-be-used', - supportAuthToken, + authTokenType: 'support', + authToken: supportAuthToken, email, accountID, }).then(() => { From e7fd9c911c9ddbbe8208605e8f3c9ed6ff2d2608 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Tue, 20 Feb 2024 17:53:33 +0100 Subject: [PATCH 23/72] prettier --- src/libs/Network/enhanceParameters.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/libs/Network/enhanceParameters.ts b/src/libs/Network/enhanceParameters.ts index 1b41dc702868..5aac99aafd41 100644 --- a/src/libs/Network/enhanceParameters.ts +++ b/src/libs/Network/enhanceParameters.ts @@ -16,10 +16,7 @@ function isAuthTokenRequired(command: string): boolean { export default function enhanceParameters(command: string, parameters: Record): Record { const finalParameters = {...parameters}; - if (isAuthTokenRequired(command) - && (NetworkStore.isSupportAuthToken() - && NetworkStore.isSupportRequest(command) - || !parameters.authToken)) { + if (isAuthTokenRequired(command) && ((NetworkStore.isSupportAuthToken() && NetworkStore.isSupportRequest(command)) || !parameters.authToken)) { finalParameters.authToken = NetworkStore.getAuthToken(); } From 2bd4030d4304c7ddc0543de53b81d6c4767c6630 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Fri, 23 Feb 2024 15:04:04 +0100 Subject: [PATCH 24/72] DRYing the types --- src/libs/Navigation/types.ts | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/src/libs/Navigation/types.ts b/src/libs/Navigation/types.ts index 5b7252a9ec71..7cd691507152 100644 --- a/src/libs/Navigation/types.ts +++ b/src/libs/Navigation/types.ts @@ -460,22 +460,27 @@ type BottomTabNavigatorParamList = { [SCREENS.WORKSPACE.INITIAL]: undefined; }; -type PublicScreensParamList = { +type SharedScreensParamList = { [NAVIGATORS.BOTTOM_TAB_NAVIGATOR]: NavigatorScreenParams; [SCREENS.TRANSITION_BETWEEN_APPS]: { - email?: string; - accountID?: number; + email: string; + accountID: number; error?: string; - supportAuthToken?: string; - shortLivedAuthToken?: string; - shortLivedToken?: string; + supportAuthToken: string; + shortLivedAuthToken: string; + shortLivedToken: string; exitTo?: Routes | HybridAppRoute; + shouldForceLogin: string; }; [SCREENS.VALIDATE_LOGIN]: { accountID: string; validateCode: string; exitTo?: Routes | HybridAppRoute; }; +} + +type PublicScreensParamList = SharedScreensParamList & { + [SCREENS.UNLINK_LOGIN]: { accountID?: string; validateCode?: string; @@ -485,21 +490,7 @@ type PublicScreensParamList = { [SCREENS.SAML_SIGN_IN]: undefined; }; -type AuthScreensParamList = { - [NAVIGATORS.BOTTOM_TAB_NAVIGATOR]: NavigatorScreenParams; - [NAVIGATORS.CENTRAL_PANE_NAVIGATOR]: NavigatorScreenParams; - [SCREENS.VALIDATE_LOGIN]: { - accountID: string; - validateCode: string; - }; - [SCREENS.TRANSITION_BETWEEN_APPS]: { - shouldForceLogin: string; - email: string; - shortLivedAuthToken: string; - supportAuthToken: string; - accountID: number; - exitTo: string; - }; +type AuthScreensParamList = SharedScreensParamList & { [SCREENS.CONCIERGE]: undefined; [SCREENS.REPORT_ATTACHMENTS]: { reportID: string; From 28263074c88231cab23a3bf388236f54b9f45fef Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Fri, 23 Feb 2024 15:08:56 +0100 Subject: [PATCH 25/72] adding token types to the const file --- src/CONST.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/CONST.ts b/src/CONST.ts index df0e1ad1c4ee..8dd8489152c7 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -72,6 +72,11 @@ const CONST = { FAILED: 'failed', }, + AUTH_TOKEN_TYPES: { + ANONYMOUS: 'anonymousAccount', + SUPPORT: 'support', + }, + AVATAR_MAX_ATTACHMENT_SIZE: 6291456, AVATAR_ALLOWED_EXTENSIONS: ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg'], From b00fe7beffc8b4927c5a9315a20b5e8629109f59 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Fri, 23 Feb 2024 15:09:18 +0100 Subject: [PATCH 26/72] changing method to isSupportTAuthToken to check the type from const --- src/libs/Network/NetworkStore.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libs/Network/NetworkStore.ts b/src/libs/Network/NetworkStore.ts index 5b1ad0eac89b..c880e3d6a6ac 100644 --- a/src/libs/Network/NetworkStore.ts +++ b/src/libs/Network/NetworkStore.ts @@ -2,10 +2,12 @@ import Onyx from 'react-native-onyx'; import {READ_COMMANDS, SIDE_EFFECT_REQUEST_COMMANDS} from '@libs/API/types'; import ONYXKEYS from '@src/ONYXKEYS'; import type Credentials from '@src/types/onyx/Credentials'; +import CONST from '@src/CONST'; +import type { ValueOf } from 'type-fest'; let credentials: Credentials | null = null; let authToken: string | null = null; -let authTokenType: string | null = null; +let authTokenType: ValueOf | null; let currentUserEmail: string | null = null; let offline = false; let authenticating = false; @@ -100,7 +102,7 @@ function isSupportRequest(command: string): boolean { } function isSupportAuthToken(): boolean { - return authTokenType === 'support'; + return authTokenType === CONST.AUTH_TOKEN_TYPES.SUPPORT; } function setAuthToken(newAuthToken: string | null) { From d7400f42b5b130aeb0653dd56d8f0b5ed4a3b4a2 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Fri, 23 Feb 2024 15:13:44 +0100 Subject: [PATCH 27/72] changing type to use valueOF const --- src/types/onyx/Session.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/onyx/Session.ts b/src/types/onyx/Session.ts index 88417a03d903..d181114d02d3 100644 --- a/src/types/onyx/Session.ts +++ b/src/types/onyx/Session.ts @@ -12,7 +12,7 @@ type Session = { authToken?: string; /** Currently logged in user authToken type */ - authTokenType?: string; + authTokenType?: ValueOf; /** Currently logged in user support authToken */ supportAuthToken?: string; From 8dc2f7e705b17111a26ec8c87d053858807975c9 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Fri, 23 Feb 2024 15:13:54 +0100 Subject: [PATCH 28/72] changing usages to constant --- src/libs/actions/Session/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 9a88013e93c4..4eb4ef2c08f7 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -53,7 +53,7 @@ import type Credentials from '@src/types/onyx/Credentials'; import type {AutoAuthState} from '@src/types/onyx/Session'; import clearCache from './clearCache'; -let sessionAuthTokenType: string | null = ''; +let sessionAuthTokenType: ValueOf | null = null; let sessionAuthToken: string | null = null; let authPromiseResolver: ((value: boolean) => void) | null = null; @@ -83,7 +83,7 @@ Onyx.connect({ }); function isSupportalToken(): boolean { - return sessionAuthTokenType === 'supportal'; + return sessionAuthTokenType === CONST.AUTH_TOKEN_TYPES.SUPPORT; } /** From 61f0b39923dff91b89daa8b261bddd5fd9d16dc9 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Fri, 23 Feb 2024 15:14:25 +0100 Subject: [PATCH 29/72] updating usage to const --- src/libs/ReportUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index d0c3bf3e8c03..49cf39e7553e 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -436,7 +436,7 @@ Onyx.connect({ currentUserEmail = value.email; currentUserAccountID = value.accountID; - isAnonymousUser = value.authTokenType === 'anonymousAccount'; + isAnonymousUser = value.authTokenType === CONST.AUTH_TOKEN_TYPES.ANONYMOUS; }, }); From 0956d292fdb08071acdcc97946a8f37f9154cc0c Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Fri, 23 Feb 2024 15:19:48 +0100 Subject: [PATCH 30/72] separate set and clear support auth token --- src/libs/actions/Session/index.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 4eb4ef2c08f7..f7a2b2202401 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -87,8 +87,7 @@ function isSupportalToken(): boolean { } /** - * Sets the SupportToken. This method will only be used on SignOut when an user - * is using a token of type support or on dev. + * Sets the SupportToken. This method will only be used on dev. */ function setSupportAuthToken(supportAuthToken: string, email?: string, accountID?: number) { if (supportAuthToken) { @@ -100,14 +99,17 @@ function setSupportAuthToken(supportAuthToken: string, email?: string, accountID }).then(() => { Log.info('[Supportal] Authtoken set'); }); - } else { - Onyx.set(ONYXKEYS.SESSION, {}).then(() => { - Log.info('[Supportal] Authtoken removed'); - }); } Onyx.set(ONYXKEYS.LAST_VISITED_PATH, ''); } +function clearSupportAuthToken() { + Onyx.set(ONYXKEYS.SESSION, null).then(() => { + Log.info('[Supportal] Authtoken removed'); + }); + Onyx.set(ONYXKEYS.LAST_VISITED_PATH, null); +} + function getShortLivedLoginParams() { const optimisticData: OnyxUpdate[] = [ { @@ -168,7 +170,7 @@ function signOut() { // In case this is a supportal token, we won't have infinite sessions setup since the token will be // short lived. So we can just remove the token and we can just skip calling logout. if (isSupportalToken()) { - setSupportAuthToken(''); + clearSupportAuthToken(); } else { const params: LogOutParams = { // Send current authToken because we will immediately clear it once triggering this command From d13664219a10eb4a7ce0f0aa9ddf62fb556fd198 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Fri, 23 Feb 2024 15:20:24 +0100 Subject: [PATCH 31/72] prettier --- src/libs/Navigation/types.ts | 3 +-- src/libs/Network/NetworkStore.ts | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/libs/Navigation/types.ts b/src/libs/Navigation/types.ts index 7cd691507152..c4b857c7b15b 100644 --- a/src/libs/Navigation/types.ts +++ b/src/libs/Navigation/types.ts @@ -477,10 +477,9 @@ type SharedScreensParamList = { validateCode: string; exitTo?: Routes | HybridAppRoute; }; -} +}; type PublicScreensParamList = SharedScreensParamList & { - [SCREENS.UNLINK_LOGIN]: { accountID?: string; validateCode?: string; diff --git a/src/libs/Network/NetworkStore.ts b/src/libs/Network/NetworkStore.ts index c880e3d6a6ac..fe76826781bd 100644 --- a/src/libs/Network/NetworkStore.ts +++ b/src/libs/Network/NetworkStore.ts @@ -1,13 +1,13 @@ import Onyx from 'react-native-onyx'; +import type {ValueOf} from 'type-fest'; import {READ_COMMANDS, SIDE_EFFECT_REQUEST_COMMANDS} from '@libs/API/types'; +import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type Credentials from '@src/types/onyx/Credentials'; -import CONST from '@src/CONST'; -import type { ValueOf } from 'type-fest'; let credentials: Credentials | null = null; let authToken: string | null = null; -let authTokenType: ValueOf | null; +let authTokenType: ValueOf | null; let currentUserEmail: string | null = null; let offline = false; let authenticating = false; From 6eada6e20cf59bd3f3a0b61642b280cf3be14975 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Fri, 23 Feb 2024 15:22:24 +0100 Subject: [PATCH 32/72] using finally data --- src/libs/actions/Session/index.ts | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index f7a2b2202401..f92b5dcb7d3c 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -132,7 +132,7 @@ function getShortLivedLoginParams() { // Subsequently, we revert it back to the default value of 'signedInWithShortLivedAuthToken' in 'successData' or 'failureData' to ensure the user is logged out on refresh // We are combining both success and failure data params into one const as they are identical - const resolutionData: OnyxUpdate[] = [ + const finallyData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.ACCOUNT, @@ -149,16 +149,16 @@ function getShortLivedLoginParams() { }, ]; - return {optimisticData, resolutionData}; + return {optimisticData, finallyData}; } /** * This method should be used when we are being redirected from oldDot to NewDot on a supportal request */ function signInWithSupportAuthToken(authToken: string) { - const {optimisticData, resolutionData} = getShortLivedLoginParams(); + const {optimisticData, finallyData} = getShortLivedLoginParams(); const params: SignInWithSupportAuthTokenParams = {authToken}; - API.read(READ_COMMANDS.SIGN_IN_WITH_SUPPORT_AUTH_TOKEN, params, {optimisticData, successData: resolutionData, failureData: resolutionData}); + API.read(READ_COMMANDS.SIGN_IN_WITH_SUPPORT_AUTH_TOKEN, params, {optimisticData, finallyData}); } /** @@ -401,10 +401,7 @@ function beginGoogleSignIn(token: string | null) { * re-authenticating after an authToken expires. */ function signInWithShortLivedAuthToken(email: string, authToken: string) { - const {optimisticData, resolutionData} = getShortLivedLoginParams(); - - const successData = resolutionData; - const failureData = resolutionData; + const {optimisticData, finallyData} = getShortLivedLoginParams(); // If the user is signing in with a different account from the current app, should not pass the auto-generated login as it may be tied to the old account. // scene 1: the user is transitioning to newDot from a different account on oldDot. @@ -413,7 +410,7 @@ function signInWithShortLivedAuthToken(email: string, authToken: string) { const params: SignInWithShortLivedAuthTokenParams = {authToken, oldPartnerUserID, skipReauthentication: true}; - API.read(READ_COMMANDS.SIGN_IN_WITH_SHORT_LIVED_AUTH_TOKEN, params, {optimisticData, successData, failureData}); + API.read(READ_COMMANDS.SIGN_IN_WITH_SHORT_LIVED_AUTH_TOKEN, params, {optimisticData, finallyData}); } /** From 286d4b828080ac8e540dd629f17d1c7a86ec9f85 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Fri, 23 Feb 2024 15:23:31 +0100 Subject: [PATCH 33/72] addressing the last reference to supportal to use const --- src/libs/actions/Session/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index f92b5dcb7d3c..179bc3bf2891 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -92,7 +92,7 @@ function isSupportalToken(): boolean { function setSupportAuthToken(supportAuthToken: string, email?: string, accountID?: number) { if (supportAuthToken) { Onyx.merge(ONYXKEYS.SESSION, { - authTokenType: 'support', + authTokenType: CONST.AUTH_TOKEN_TYPES.SUPPORT, authToken: supportAuthToken, email, accountID, From 0e03fc5f2a9483f579091281afc57895b9e4659f Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Fri, 23 Feb 2024 15:39:09 +0100 Subject: [PATCH 34/72] changing inheritance to reference --- src/libs/Navigation/types.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/libs/Navigation/types.ts b/src/libs/Navigation/types.ts index c4b857c7b15b..5816611a16a9 100644 --- a/src/libs/Navigation/types.ts +++ b/src/libs/Navigation/types.ts @@ -479,7 +479,10 @@ type SharedScreensParamList = { }; }; -type PublicScreensParamList = SharedScreensParamList & { +type PublicScreensParamList = { + [NAVIGATORS.BOTTOM_TAB_NAVIGATOR]: ValueOf; + [SCREENS.TRANSITION_BETWEEN_APPS]: ValueOf; + [SCREENS.VALIDATE_LOGIN]: ValueOf; [SCREENS.UNLINK_LOGIN]: { accountID?: string; validateCode?: string; @@ -489,7 +492,10 @@ type PublicScreensParamList = SharedScreensParamList & { [SCREENS.SAML_SIGN_IN]: undefined; }; -type AuthScreensParamList = SharedScreensParamList & { +type AuthScreensParamList = { + [NAVIGATORS.BOTTOM_TAB_NAVIGATOR]: ValueOf; + [SCREENS.TRANSITION_BETWEEN_APPS]: ValueOf; + [SCREENS.VALIDATE_LOGIN]: ValueOf; [SCREENS.CONCIERGE]: undefined; [SCREENS.REPORT_ATTACHMENTS]: { reportID: string; From 533bff2b8343bff3a43110704adff0696206e657 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Fri, 23 Feb 2024 16:15:52 +0100 Subject: [PATCH 35/72] undo changes to ref --- src/libs/Navigation/types.ts | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/libs/Navigation/types.ts b/src/libs/Navigation/types.ts index 5816611a16a9..c4b857c7b15b 100644 --- a/src/libs/Navigation/types.ts +++ b/src/libs/Navigation/types.ts @@ -479,10 +479,7 @@ type SharedScreensParamList = { }; }; -type PublicScreensParamList = { - [NAVIGATORS.BOTTOM_TAB_NAVIGATOR]: ValueOf; - [SCREENS.TRANSITION_BETWEEN_APPS]: ValueOf; - [SCREENS.VALIDATE_LOGIN]: ValueOf; +type PublicScreensParamList = SharedScreensParamList & { [SCREENS.UNLINK_LOGIN]: { accountID?: string; validateCode?: string; @@ -492,10 +489,7 @@ type PublicScreensParamList = { [SCREENS.SAML_SIGN_IN]: undefined; }; -type AuthScreensParamList = { - [NAVIGATORS.BOTTOM_TAB_NAVIGATOR]: ValueOf; - [SCREENS.TRANSITION_BETWEEN_APPS]: ValueOf; - [SCREENS.VALIDATE_LOGIN]: ValueOf; +type AuthScreensParamList = SharedScreensParamList & { [SCREENS.CONCIERGE]: undefined; [SCREENS.REPORT_ATTACHMENTS]: { reportID: string; From f1772a02967bcb55ec088fde6fac1cd7acb98ae4 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Sat, 24 Feb 2024 18:53:02 +0100 Subject: [PATCH 36/72] readding optional parameters and fixing routes to include central panel again --- src/libs/Navigation/types.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libs/Navigation/types.ts b/src/libs/Navigation/types.ts index c4b857c7b15b..94b51e0f7009 100644 --- a/src/libs/Navigation/types.ts +++ b/src/libs/Navigation/types.ts @@ -466,9 +466,9 @@ type SharedScreensParamList = { email: string; accountID: number; error?: string; - supportAuthToken: string; - shortLivedAuthToken: string; - shortLivedToken: string; + supportAuthToken?: string; + shortLivedAuthToken?: string; + shortLivedToken?: string; exitTo?: Routes | HybridAppRoute; shouldForceLogin: string; }; @@ -490,6 +490,7 @@ type PublicScreensParamList = SharedScreensParamList & { }; type AuthScreensParamList = SharedScreensParamList & { + [NAVIGATORS.CENTRAL_PANE_NAVIGATOR]: NavigatorScreenParams; [SCREENS.CONCIERGE]: undefined; [SCREENS.REPORT_ATTACHMENTS]: { reportID: string; From 42e02951fbe695a0fd80026057ab6a82a40bc7d4 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Sat, 24 Feb 2024 18:55:20 +0100 Subject: [PATCH 37/72] removing explicity typing --- src/libs/actions/Session/index.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 179bc3bf2891..2b03a5e3c60e 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -12,13 +12,11 @@ import type { BeginAppleSignInParams, BeginGoogleSignInParams, BeginSignInParams, - LogOutParams, RequestAccountValidationLinkParams, RequestNewValidateCodeParams, RequestUnlinkValidationLinkParams, SignInUserWithLinkParams, SignInWithShortLivedAuthTokenParams, - SignInWithSupportAuthTokenParams, UnlinkLoginParams, ValidateTwoFactorAuthParams, } from '@libs/API/parameters'; @@ -157,8 +155,7 @@ function getShortLivedLoginParams() { */ function signInWithSupportAuthToken(authToken: string) { const {optimisticData, finallyData} = getShortLivedLoginParams(); - const params: SignInWithSupportAuthTokenParams = {authToken}; - API.read(READ_COMMANDS.SIGN_IN_WITH_SUPPORT_AUTH_TOKEN, params, {optimisticData, finallyData}); + API.read(READ_COMMANDS.SIGN_IN_WITH_SUPPORT_AUTH_TOKEN, {authToken}, {optimisticData, finallyData}); } /** @@ -172,7 +169,7 @@ function signOut() { if (isSupportalToken()) { clearSupportAuthToken(); } else { - const params: LogOutParams = { + const params = { // Send current authToken because we will immediately clear it once triggering this command authToken: NetworkStore.getAuthToken(), partnerUserID: credentials?.autoGeneratedLogin ?? '', @@ -407,10 +404,7 @@ function signInWithShortLivedAuthToken(email: string, authToken: string) { // scene 1: the user is transitioning to newDot from a different account on oldDot. // scene 2: the user is transitioning to desktop app from a different account on web app. const oldPartnerUserID = credentials.login === email && credentials.autoGeneratedLogin ? credentials.autoGeneratedLogin : ''; - - const params: SignInWithShortLivedAuthTokenParams = {authToken, oldPartnerUserID, skipReauthentication: true}; - - API.read(READ_COMMANDS.SIGN_IN_WITH_SHORT_LIVED_AUTH_TOKEN, params, {optimisticData, finallyData}); + API.read(READ_COMMANDS.SIGN_IN_WITH_SHORT_LIVED_AUTH_TOKEN, {authToken, oldPartnerUserID, skipReauthentication: true}, {optimisticData, finallyData}); } /** From aa312965c871dda63b9c8a3216d7c188040ff835 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Wed, 28 Feb 2024 18:37:16 +0100 Subject: [PATCH 38/72] using new parameter of authtokentype --- src/libs/Navigation/types.ts | 2 +- src/pages/LogInWithShortLivedAuthTokenPage.tsx | 7 ++++--- src/pages/LogOutPreviousUserPage.tsx | 5 +++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/libs/Navigation/types.ts b/src/libs/Navigation/types.ts index e6d33d86cc50..64855da0919f 100644 --- a/src/libs/Navigation/types.ts +++ b/src/libs/Navigation/types.ts @@ -503,9 +503,9 @@ type SharedScreensParamList = { email: string; accountID: number; error?: string; - supportAuthToken?: string; shortLivedAuthToken?: string; shortLivedToken?: string; + authTokenType?: ValueOf; exitTo?: Routes | HybridAppRoute; shouldForceLogin: string; domain?: Routes; diff --git a/src/pages/LogInWithShortLivedAuthTokenPage.tsx b/src/pages/LogInWithShortLivedAuthTokenPage.tsx index 7631a93eaba2..007cd5ef36f7 100644 --- a/src/pages/LogInWithShortLivedAuthTokenPage.tsx +++ b/src/pages/LogInWithShortLivedAuthTokenPage.tsx @@ -19,6 +19,7 @@ import * as Session from '@userActions/Session'; import ONYXKEYS from '@src/ONYXKEYS'; import type SCREENS from '@src/SCREENS'; import type {Account} from '@src/types/onyx'; +import CONST from '@src/CONST'; type LogInWithShortLivedAuthTokenPageOnyxProps = { /** The details about the account that the user is signing in with */ @@ -31,7 +32,7 @@ function LogInWithShortLivedAuthTokenPage({route, account}: LogInWithShortLivedA const theme = useTheme(); const styles = useThemeStyles(); const {translate} = useLocalize(); - const {email = '', supportAuthToken = '', shortLivedAuthToken = '', shortLivedToken = '', exitTo, error} = route?.params ?? {}; + const {email = '', shortLivedAuthToken = '', shortLivedToken = '', authTokenType, exitTo, error} = route?.params ?? {}; useEffect(() => { // We have to check for both shortLivedAuthToken and shortLivedToken, as the old mobile app uses shortLivedToken, and is not being actively updated. @@ -44,8 +45,8 @@ function LogInWithShortLivedAuthTokenPage({route, account}: LogInWithShortLivedA return; } - if (!account?.isLoading && supportAuthToken) { - Session.signInWithSupportAuthToken(supportAuthToken); + if (!account?.isLoading && authTokenType === CONST.AUTH_TOKEN_TYPES.SUPPORT) { + Session.signInWithSupportAuthToken(shortLivedAuthToken); return; } diff --git a/src/pages/LogOutPreviousUserPage.tsx b/src/pages/LogOutPreviousUserPage.tsx index d69286232072..601266892641 100644 --- a/src/pages/LogOutPreviousUserPage.tsx +++ b/src/pages/LogOutPreviousUserPage.tsx @@ -10,6 +10,7 @@ import * as SessionActions from '@userActions/Session'; import ONYXKEYS from '@src/ONYXKEYS'; import type SCREENS from '@src/SCREENS'; import type {Session} from '@src/types/onyx'; +import CONST from '@src/CONST'; type LogOutPreviousUserPageOnyxProps = { /** The data about the current session which will be set once the user is authenticated and we return to this component as an AuthScreen */ @@ -32,8 +33,8 @@ function LogOutPreviousUserPage({session, route}: LogOutPreviousUserPageProps) { SessionActions.signOutAndRedirectToSignIn(); } - if (route.params.supportAuthToken !== '') { - SessionActions.signInWithSupportAuthToken(route.params.supportAuthToken); + if (route.params.authTokenType === CONST.AUTH_TOKEN_TYPES.SUPPORT) { + SessionActions.signInWithSupportAuthToken(route.params.shortLivedAuthToken ?? ''); return; } From 7bd9d4b4f515b0f82a17dbf8ad6e9b72abd871b6 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Thu, 29 Feb 2024 14:06:30 +0100 Subject: [PATCH 39/72] adding key for new translated item --- src/languages/en.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/languages/en.ts b/src/languages/en.ts index cf8823f5b2be..5be080688d4e 100755 --- a/src/languages/en.ts +++ b/src/languages/en.ts @@ -862,6 +862,7 @@ export default { }, security: 'Security', signOut: 'Sign out', + restoreStashed: 'Restore stashed login', signOutConfirmationText: "You'll lose any offline changes if you sign-out.", versionLetter: 'v', readTheTermsAndPrivacy: { From 1c8bcf162400cd424e070d3bdf78c0dd7ba46c07 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Thu, 29 Feb 2024 14:06:58 +0100 Subject: [PATCH 40/72] exporting function to check if it's supportal --- src/libs/actions/Session/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index f9e1d4c8a0ce..395a27cabea6 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -955,4 +955,5 @@ export { waitForUserSignIn, canAccessRouteByAnonymousUser, signInWithSupportAuthToken, + isSupportalToken, }; From 2a341208cb9c9b3851a4e8559e06b097e1e3dc39 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Thu, 29 Feb 2024 14:07:23 +0100 Subject: [PATCH 41/72] changing text based on which type of token we're using --- src/pages/settings/InitialSettingsPage.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pages/settings/InitialSettingsPage.tsx b/src/pages/settings/InitialSettingsPage.tsx index 2f21ee61e8cc..fb2c119c7188 100755 --- a/src/pages/settings/InitialSettingsPage.tsx +++ b/src/pages/settings/InitialSettingsPage.tsx @@ -129,7 +129,7 @@ function InitialSettingsPage({session, userWallet, bankAccountList, fundList, wa const accountMenuItemsData: Menu = useMemo(() => { const profileBrickRoadIndicator = UserUtils.getLoginListBrickRoadIndicator(loginList); const paymentCardList = fundList; - + const signOutTranslationKey = Session.isSupportalToken() ? 'initialSettingsPage.signOut' : 'initialSettingsPage.restoreStashed'; const defaultMenu: Menu = { sectionStyle: styles.accountSettingsSectionContainer, sectionTranslationKey: 'initialSettingsPage.account', @@ -165,7 +165,7 @@ function InitialSettingsPage({session, userWallet, bankAccountList, fundList, wa routeName: ROUTES.SETTINGS_SECURITY, }, { - translationKey: 'initialSettingsPage.signOut', + translationKey: signOutTranslationKey, icon: Expensicons.Exit, action: () => { signOut(false); From c052286ebeffe37c2457639992f06f4602425c05 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Thu, 29 Feb 2024 14:22:18 +0100 Subject: [PATCH 42/72] creating new keys for stashed sessiond ata --- src/ONYXKEYS.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ONYXKEYS.ts b/src/ONYXKEYS.ts index 78f0e61e72a9..dde194b553ce 100755 --- a/src/ONYXKEYS.ts +++ b/src/ONYXKEYS.ts @@ -40,6 +40,7 @@ const ONYXKEYS = { /** Credentials to authenticate the user */ CREDENTIALS: 'credentials', + STASHED_CREDENTIALS: 'stashedCredentials', // Contains loading data for the IOU feature (MoneyRequestModal, IOUDetail, & MoneyRequestPreview Components) IOU: 'iou', @@ -102,6 +103,7 @@ const ONYXKEYS = { /** Information about the current session (authToken, accountID, email, loading, error) */ SESSION: 'session', + STASHED_SESSION: 'stashedSession', BETAS: 'betas', /** NVP keys From 65ad0abd75dcb00b70b98a855a62a073bb9c6b9e Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Thu, 29 Feb 2024 14:24:06 +0100 Subject: [PATCH 43/72] mapping new keys --- src/ONYXKEYS.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ONYXKEYS.ts b/src/ONYXKEYS.ts index dde194b553ce..6af593d213f0 100755 --- a/src/ONYXKEYS.ts +++ b/src/ONYXKEYS.ts @@ -490,6 +490,7 @@ type OnyxValuesMapping = { [ONYXKEYS.PERSISTED_REQUESTS]: OnyxTypes.Request[]; [ONYXKEYS.CURRENT_DATE]: string; [ONYXKEYS.CREDENTIALS]: OnyxTypes.Credentials; + [ONYXKEYS.STASHED_CREDENTIALS]: OnyxTypes.Credentials; [ONYXKEYS.IOU]: OnyxTypes.IOU; [ONYXKEYS.MODAL]: OnyxTypes.Modal; [ONYXKEYS.NETWORK]: OnyxTypes.Network; @@ -508,6 +509,7 @@ type OnyxValuesMapping = { [ONYXKEYS.USER_LOCATION]: OnyxTypes.UserLocation; [ONYXKEYS.LOGIN_LIST]: OnyxTypes.LoginList; [ONYXKEYS.SESSION]: OnyxTypes.Session; + [ONYXKEYS.STASHED_SESSION]: OnyxTypes.Session; [ONYXKEYS.BETAS]: OnyxTypes.Beta[]; [ONYXKEYS.NVP_PRIORITY_MODE]: ValueOf; [ONYXKEYS.NVP_BLOCKED_FROM_CONCIERGE]: OnyxTypes.BlockedFromConcierge; From 4258128514f788f44fa2416c994da49b1cf46974 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Thu, 29 Feb 2024 14:25:00 +0100 Subject: [PATCH 44/72] ignoring new keys when clearing the storage --- src/libs/actions/SignInRedirect.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libs/actions/SignInRedirect.ts b/src/libs/actions/SignInRedirect.ts index ecb09ccd1804..f8e13e3d1f3c 100644 --- a/src/libs/actions/SignInRedirect.ts +++ b/src/libs/actions/SignInRedirect.ts @@ -21,6 +21,8 @@ function clearStorageAndRedirect(errorMessage?: string) { keysToPreserve.push(ONYXKEYS.NVP_PREFERRED_LOCALE); keysToPreserve.push(ONYXKEYS.ACTIVE_CLIENTS); keysToPreserve.push(ONYXKEYS.DEVICE_ID); + keysToPreserve.push(ONYXKEYS.STASHED_CREDENTIALS); + keysToPreserve.push(ONYXKEYS.STASHED_SESSION); // After signing out, set ourselves as offline if we were offline before logging out and we are not forcing it. // If we are forcing offline, ignore it while signed out, otherwise it would require a refresh because there's no way to toggle the switch to go back online while signed out. From 226f6aa328e6d319f059428361c5a253388046ef Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Thu, 29 Feb 2024 14:41:37 +0100 Subject: [PATCH 45/72] adding logic to restore stashed login --- src/libs/actions/Session/index.ts | 48 ++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 395a27cabea6..2f5c9f786bb4 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -16,7 +16,6 @@ import type { RequestNewValidateCodeParams, RequestUnlinkValidationLinkParams, SignInUserWithLinkParams, - SignInWithShortLivedAuthTokenParams, UnlinkLoginParams, ValidateTwoFactorAuthParams, } from '@libs/API/parameters'; @@ -50,6 +49,7 @@ import SCREENS from '@src/SCREENS'; import type Credentials from '@src/types/onyx/Credentials'; import type {AutoAuthState} from '@src/types/onyx/Session'; import clearCache from './clearCache'; +import Session from '@src/types/onyx/Session'; let sessionAuthTokenType: ValueOf | null = null; let sessionAuthToken: string | null = null; @@ -74,6 +74,18 @@ Onyx.connect({ callback: (value) => (credentials = value ?? {}), }); +let stashedCredentials: Credentials = {}; +Onyx.connect({ + key: ONYXKEYS.STASHED_CREDENTIALS, + callback: (value) => (stashedCredentials = value ?? {}), +}); + +let stashedSession: Session = {}; +Onyx.connect({ + key: ONYXKEYS.STASHED_SESSION, + callback: (value) => (stashedSession = value ?? {}), +}); + let preferredLocale: ValueOf | null = null; Onyx.connect({ key: ONYXKEYS.NVP_PREFERRED_LOCALE, @@ -87,17 +99,15 @@ function isSupportalToken(): boolean { /** * Sets the SupportToken. This method will only be used on dev. */ -function setSupportAuthToken(supportAuthToken: string, email?: string, accountID?: number) { - if (supportAuthToken) { - Onyx.merge(ONYXKEYS.SESSION, { - authTokenType: CONST.AUTH_TOKEN_TYPES.SUPPORT, - authToken: supportAuthToken, - email, - accountID, - }).then(() => { - Log.info('[Supportal] Authtoken set'); - }); - } +function setSupportAuthToken(supportAuthToken: string, email: string, accountID: number) { + Onyx.merge(ONYXKEYS.SESSION, { + authTokenType: CONST.AUTH_TOKEN_TYPES.SUPPORT, + authToken: supportAuthToken, + email, + accountID, + }).then(() => { + Log.info('[Supportal] Authtoken set'); + }); Onyx.set(ONYXKEYS.LAST_VISITED_PATH, ''); } @@ -194,12 +204,24 @@ function isAnonymousUser(): boolean { return sessionAuthTokenType === 'anonymousAccount'; } -function signOutAndRedirectToSignIn(shouldReplaceCurrentScreen?: boolean) { +function signOutAndRedirectToSignIn(shouldReplaceCurrentScreen?: boolean, stashSession?: boolean) { Log.info('Redirecting to Sign In because signOut() was called'); hideContextMenu(false); + if (!isAnonymousUser()) { signOut(); redirectToSignIn(); + + // If someone is logged as a support user, let's check if there's any credentials stashed, and + // if there is, let's set that session as the main one instead of completely logging out. + if(isSupportalToken() && stashedCredentials.autoGeneratedLogin && stashedCredentials.autoGeneratedLogin !== '') { + Onyx.multiSet({ + [ONYXKEYS.CREDENTIALS]: stashedCredentials, + [ONYXKEYS.SESSION]: stashedSession, + [ONYXKEYS.STASHED_CREDENTIALS]: null, + [ONYXKEYS.STASHED_SESSION]: null, + }) + } } else { if (Navigation.isActiveRoute(ROUTES.SIGN_IN_MODAL)) { return; From e5b27b57ae9ba1c5df6c0b0ebbc4fe99cffb41b4 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Thu, 29 Feb 2024 14:46:55 +0100 Subject: [PATCH 46/72] passing new parameters to stash login --- src/pages/LogOutPreviousUserPage.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pages/LogOutPreviousUserPage.tsx b/src/pages/LogOutPreviousUserPage.tsx index 601266892641..39d1de600146 100644 --- a/src/pages/LogOutPreviousUserPage.tsx +++ b/src/pages/LogOutPreviousUserPage.tsx @@ -28,12 +28,13 @@ function LogOutPreviousUserPage({session, route}: LogOutPreviousUserPageProps) { Linking.getInitialURL().then((transitionURL) => { const sessionEmail = session?.email; const isLoggingInAsNewUser = SessionUtils.isLoggingInAsNewUser(transitionURL ?? undefined, sessionEmail); + const isSupportalLogin = route.params.authTokenType === CONST.AUTH_TOKEN_TYPES.SUPPORT; if (isLoggingInAsNewUser) { SessionActions.signOutAndRedirectToSignIn(); } - if (route.params.authTokenType === CONST.AUTH_TOKEN_TYPES.SUPPORT) { + if (isSupportalLogin) { SessionActions.signInWithSupportAuthToken(route.params.shortLivedAuthToken ?? ''); return; } From 47606350fb35b6e6f4470f641740df989802d328 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Thu, 29 Feb 2024 14:47:09 +0100 Subject: [PATCH 47/72] using the whole session object so we can store it easily --- src/libs/actions/Session/index.ts | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 2f5c9f786bb4..1a5b6199c09d 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -51,23 +51,26 @@ import type {AutoAuthState} from '@src/types/onyx/Session'; import clearCache from './clearCache'; import Session from '@src/types/onyx/Session'; -let sessionAuthTokenType: ValueOf | null = null; -let sessionAuthToken: string | null = null; +let session: Session = {}; let authPromiseResolver: ((value: boolean) => void) | null = null; Onyx.connect({ key: ONYXKEYS.SESSION, - callback: (session) => { - sessionAuthTokenType = session?.authTokenType ?? null; - sessionAuthToken = session?.authToken ?? null; - - if (sessionAuthToken && authPromiseResolver) { + callback: (value) => { + session = value ?? {}; + if (session.authToken && authPromiseResolver) { authPromiseResolver(true); authPromiseResolver = null; } }, }); +let stashedSession: Session = {}; +Onyx.connect({ + key: ONYXKEYS.STASHED_SESSION, + callback: (value) => (stashedSession = value ?? {}), +}); + let credentials: Credentials = {}; Onyx.connect({ key: ONYXKEYS.CREDENTIALS, @@ -80,12 +83,6 @@ Onyx.connect({ callback: (value) => (stashedCredentials = value ?? {}), }); -let stashedSession: Session = {}; -Onyx.connect({ - key: ONYXKEYS.STASHED_SESSION, - callback: (value) => (stashedSession = value ?? {}), -}); - let preferredLocale: ValueOf | null = null; Onyx.connect({ key: ONYXKEYS.NVP_PREFERRED_LOCALE, @@ -93,7 +90,7 @@ Onyx.connect({ }); function isSupportalToken(): boolean { - return sessionAuthTokenType === CONST.AUTH_TOKEN_TYPES.SUPPORT; + return session.authTokenType === CONST.AUTH_TOKEN_TYPES.SUPPORT; } /** @@ -201,7 +198,7 @@ function signOut() { * Checks if the account is an anonymous account. */ function isAnonymousUser(): boolean { - return sessionAuthTokenType === 'anonymousAccount'; + return session.authTokenType === 'anonymousAccount'; } function signOutAndRedirectToSignIn(shouldReplaceCurrentScreen?: boolean, stashSession?: boolean) { @@ -890,7 +887,7 @@ function validateTwoFactorAuth(twoFactorAuthCode: string) { */ function waitForUserSignIn(): Promise { return new Promise((resolve) => { - if (sessionAuthToken) { + if (session.authToken) { resolve(true); } else { authPromiseResolver = resolve; From 0930f3c6208f4422890ead3ddf114a7e24077341 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Thu, 29 Feb 2024 15:01:02 +0100 Subject: [PATCH 48/72] logic should work --- src/libs/actions/Session/index.ts | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 1a5b6199c09d..caf8d60f315f 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -53,7 +53,6 @@ import Session from '@src/types/onyx/Session'; let session: Session = {}; let authPromiseResolver: ((value: boolean) => void) | null = null; - Onyx.connect({ key: ONYXKEYS.SESSION, callback: (value) => { @@ -201,23 +200,43 @@ function isAnonymousUser(): boolean { return session.authTokenType === 'anonymousAccount'; } +function hasStashedSession(): boolean { + return Boolean(stashedSession.authToken && stashedCredentials.autoGeneratedLogin && stashedCredentials.autoGeneratedLogin !== ''); +} + function signOutAndRedirectToSignIn(shouldReplaceCurrentScreen?: boolean, stashSession?: boolean) { Log.info('Redirecting to Sign In because signOut() was called'); hideContextMenu(false); if (!isAnonymousUser()) { - signOut(); + // If we are not currently using a support token, and we received stashSession as true, we need to + // store the credentials so the user doesn't need to login again after they finish their supportal + // action. This needs to happen before we call `redirectToSignIn` + const isSupportal = isSupportalToken(); + if(!isSupportal && stashSession) { + Onyx.multiSet({ + [ONYXKEYS.STASHED_CREDENTIALS]: credentials, + [ONYXKEYS.STASHED_SESSION]: session, + }); + } + // We'll only call signOut if we're not stashing the session, otherwise we'll call the API to invalidate + // the autogenerated credentials used for infinite sessions + if (!stashSession) { + signOut(); + } + // This function will clear the whole storage redirectToSignIn(); - // If someone is logged as a support user, let's check if there's any credentials stashed, and + // If someone was logged as a supportal user, let's check if there's any credentials stashed, and // if there is, let's set that session as the main one instead of completely logging out. - if(isSupportalToken() && stashedCredentials.autoGeneratedLogin && stashedCredentials.autoGeneratedLogin !== '') { + if(isSupportal && hasStashedSession()) { Onyx.multiSet({ [ONYXKEYS.CREDENTIALS]: stashedCredentials, [ONYXKEYS.SESSION]: stashedSession, + // Let's also remove the stashed session/credentials, since it's not needed anymore [ONYXKEYS.STASHED_CREDENTIALS]: null, [ONYXKEYS.STASHED_SESSION]: null, - }) + }); } } else { if (Navigation.isActiveRoute(ROUTES.SIGN_IN_MODAL)) { @@ -975,4 +994,5 @@ export { canAccessRouteByAnonymousUser, signInWithSupportAuthToken, isSupportalToken, + hasStashedSession, }; From 54db30bbd9a2f00b99560ca256c77cf8d80521a2 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Thu, 29 Feb 2024 15:01:22 +0100 Subject: [PATCH 49/72] adding one more check on the translation key --- src/pages/settings/InitialSettingsPage.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/settings/InitialSettingsPage.tsx b/src/pages/settings/InitialSettingsPage.tsx index fb2c119c7188..11daa4d6d7cc 100755 --- a/src/pages/settings/InitialSettingsPage.tsx +++ b/src/pages/settings/InitialSettingsPage.tsx @@ -129,7 +129,7 @@ function InitialSettingsPage({session, userWallet, bankAccountList, fundList, wa const accountMenuItemsData: Menu = useMemo(() => { const profileBrickRoadIndicator = UserUtils.getLoginListBrickRoadIndicator(loginList); const paymentCardList = fundList; - const signOutTranslationKey = Session.isSupportalToken() ? 'initialSettingsPage.signOut' : 'initialSettingsPage.restoreStashed'; + const signOutTranslationKey = Session.isSupportalToken() && Session.hasStashedSession() ? 'initialSettingsPage.signOut' : 'initialSettingsPage.restoreStashed'; const defaultMenu: Menu = { sectionStyle: styles.accountSettingsSectionContainer, sectionTranslationKey: 'initialSettingsPage.account', From 463fcb4484ea9dae97712990c789f2f73dffe169 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Thu, 29 Feb 2024 15:23:19 +0100 Subject: [PATCH 50/72] returning promise on signin redirect --- src/libs/actions/SignInRedirect.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/SignInRedirect.ts b/src/libs/actions/SignInRedirect.ts index f8e13e3d1f3c..9f4ec3b521f9 100644 --- a/src/libs/actions/SignInRedirect.ts +++ b/src/libs/actions/SignInRedirect.ts @@ -13,7 +13,7 @@ Onyx.connect({ }, }); -function clearStorageAndRedirect(errorMessage?: string) { +function clearStorageAndRedirect(errorMessage?: string) : Promise { // Under certain conditions, there are key-values we'd like to keep in storage even when a user is logged out. // We pass these into the clear() method in order to avoid having to reset them on a delayed tick and getting // flashes of unwanted default state. @@ -30,7 +30,7 @@ function clearStorageAndRedirect(errorMessage?: string) { keysToPreserve.push(ONYXKEYS.NETWORK); } - Onyx.clear(keysToPreserve).then(() => { + return Onyx.clear(keysToPreserve).then(() => { if (!errorMessage) { return; } @@ -48,8 +48,8 @@ function clearStorageAndRedirect(errorMessage?: string) { * * @param [errorMessage] error message to be displayed on the sign in page */ -function redirectToSignIn(errorMessage?: string) { - clearStorageAndRedirect(errorMessage); +function redirectToSignIn(errorMessage?: string) : Promise { + return clearStorageAndRedirect(errorMessage); } export default redirectToSignIn; From caf1cf64bd766c49cdc27ec7df5e8d254c5de69f Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Thu, 29 Feb 2024 15:23:55 +0100 Subject: [PATCH 51/72] using promise to wait before setting the stashed data on session and credentials --- src/libs/actions/Session/index.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index caf8d60f315f..df3abd54e8cd 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -225,17 +225,19 @@ function signOutAndRedirectToSignIn(shouldReplaceCurrentScreen?: boolean, stashS signOut(); } // This function will clear the whole storage - redirectToSignIn(); + let redirectPromise = redirectToSignIn(); // If someone was logged as a supportal user, let's check if there's any credentials stashed, and // if there is, let's set that session as the main one instead of completely logging out. if(isSupportal && hasStashedSession()) { - Onyx.multiSet({ - [ONYXKEYS.CREDENTIALS]: stashedCredentials, - [ONYXKEYS.SESSION]: stashedSession, - // Let's also remove the stashed session/credentials, since it's not needed anymore - [ONYXKEYS.STASHED_CREDENTIALS]: null, - [ONYXKEYS.STASHED_SESSION]: null, + redirectPromise.then(() => { + Onyx.multiSet({ + [ONYXKEYS.CREDENTIALS]: stashedCredentials, + [ONYXKEYS.SESSION]: stashedSession, + // Let's also remove the stashed session/credentials, since it's not needed anymore + [ONYXKEYS.STASHED_CREDENTIALS]: null, + [ONYXKEYS.STASHED_SESSION]: null, + }); }); } } else { From bb51ff6129ae9e9073d41ac20219499f60f2ef42 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Thu, 29 Feb 2024 15:24:18 +0100 Subject: [PATCH 52/72] fixing message that should be shown --- src/pages/settings/InitialSettingsPage.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/settings/InitialSettingsPage.tsx b/src/pages/settings/InitialSettingsPage.tsx index 11daa4d6d7cc..0adea503be67 100755 --- a/src/pages/settings/InitialSettingsPage.tsx +++ b/src/pages/settings/InitialSettingsPage.tsx @@ -129,7 +129,7 @@ function InitialSettingsPage({session, userWallet, bankAccountList, fundList, wa const accountMenuItemsData: Menu = useMemo(() => { const profileBrickRoadIndicator = UserUtils.getLoginListBrickRoadIndicator(loginList); const paymentCardList = fundList; - const signOutTranslationKey = Session.isSupportalToken() && Session.hasStashedSession() ? 'initialSettingsPage.signOut' : 'initialSettingsPage.restoreStashed'; + const signOutTranslationKey = Session.isSupportalToken() && Session.hasStashedSession() ? 'initialSettingsPage.restoreStashed' : 'initialSettingsPage.signOut'; const defaultMenu: Menu = { sectionStyle: styles.accountSettingsSectionContainer, sectionTranslationKey: 'initialSettingsPage.account', From ddc3271dc71e09977f89372cdf8c336a671d7d9d Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Thu, 29 Feb 2024 15:24:46 +0100 Subject: [PATCH 53/72] changin order on login page so we can check if it's supportal first --- src/pages/LogInWithShortLivedAuthTokenPage.tsx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/pages/LogInWithShortLivedAuthTokenPage.tsx b/src/pages/LogInWithShortLivedAuthTokenPage.tsx index 007cd5ef36f7..08804d3aa8e5 100644 --- a/src/pages/LogInWithShortLivedAuthTokenPage.tsx +++ b/src/pages/LogInWithShortLivedAuthTokenPage.tsx @@ -38,6 +38,11 @@ function LogInWithShortLivedAuthTokenPage({route, account}: LogInWithShortLivedA // We have to check for both shortLivedAuthToken and shortLivedToken, as the old mobile app uses shortLivedToken, and is not being actively updated. const token = shortLivedAuthToken || shortLivedToken; + if (!account?.isLoading && authTokenType === CONST.AUTH_TOKEN_TYPES.SUPPORT) { + Session.signInWithSupportAuthToken(shortLivedAuthToken); + return; + } + // Try to authenticate using the shortLivedToken if we're not already trying to load the accounts if (token && !account?.isLoading) { Log.info('LogInWithShortLivedAuthTokenPage - Successfully received shortLivedAuthToken. Signing in...'); @@ -45,11 +50,6 @@ function LogInWithShortLivedAuthTokenPage({route, account}: LogInWithShortLivedA return; } - if (!account?.isLoading && authTokenType === CONST.AUTH_TOKEN_TYPES.SUPPORT) { - Session.signInWithSupportAuthToken(shortLivedAuthToken); - return; - } - // If an error is returned as part of the route, ensure we set it in the onyxData for the account if (error) { Session.setAccountError(error); From 2df91801031effaccad339a4f2d6abd0b8478bfa Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Thu, 29 Feb 2024 15:25:07 +0100 Subject: [PATCH 54/72] passing parameter to store stashed login data --- src/pages/LogOutPreviousUserPage.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/LogOutPreviousUserPage.tsx b/src/pages/LogOutPreviousUserPage.tsx index 39d1de600146..0158184c8d45 100644 --- a/src/pages/LogOutPreviousUserPage.tsx +++ b/src/pages/LogOutPreviousUserPage.tsx @@ -31,7 +31,7 @@ function LogOutPreviousUserPage({session, route}: LogOutPreviousUserPageProps) { const isSupportalLogin = route.params.authTokenType === CONST.AUTH_TOKEN_TYPES.SUPPORT; if (isLoggingInAsNewUser) { - SessionActions.signOutAndRedirectToSignIn(); + SessionActions.signOutAndRedirectToSignIn(false, isSupportalLogin); } if (isSupportalLogin) { From 5a5d5bf572ec93518aca1c4c9b56139e6dbb41f9 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 4 Mar 2024 15:58:11 +0100 Subject: [PATCH 55/72] Removing stashed keys from clear storage --- src/libs/actions/SignInRedirect.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/libs/actions/SignInRedirect.ts b/src/libs/actions/SignInRedirect.ts index 9f4ec3b521f9..39c98522baa2 100644 --- a/src/libs/actions/SignInRedirect.ts +++ b/src/libs/actions/SignInRedirect.ts @@ -21,8 +21,6 @@ function clearStorageAndRedirect(errorMessage?: string) : Promise { keysToPreserve.push(ONYXKEYS.NVP_PREFERRED_LOCALE); keysToPreserve.push(ONYXKEYS.ACTIVE_CLIENTS); keysToPreserve.push(ONYXKEYS.DEVICE_ID); - keysToPreserve.push(ONYXKEYS.STASHED_CREDENTIALS); - keysToPreserve.push(ONYXKEYS.STASHED_SESSION); // After signing out, set ourselves as offline if we were offline before logging out and we are not forcing it. // If we are forcing offline, ignore it while signed out, otherwise it would require a refresh because there's no way to toggle the switch to go back online while signed out. From 0240d99ab2912285327013b041426e1dae50ca37 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 4 Mar 2024 15:58:59 +0100 Subject: [PATCH 56/72] removing transition from history on logout previous user page --- src/pages/LogOutPreviousUserPage.tsx | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/pages/LogOutPreviousUserPage.tsx b/src/pages/LogOutPreviousUserPage.tsx index 0158184c8d45..1bd28824dc06 100644 --- a/src/pages/LogOutPreviousUserPage.tsx +++ b/src/pages/LogOutPreviousUserPage.tsx @@ -11,6 +11,8 @@ import ONYXKEYS from '@src/ONYXKEYS'; import type SCREENS from '@src/SCREENS'; import type {Session} from '@src/types/onyx'; import CONST from '@src/CONST'; +import Navigation from '@libs/Navigation/Navigation'; +import ROUTES from '@src/ROUTES'; type LogOutPreviousUserPageOnyxProps = { /** The data about the current session which will be set once the user is authenticated and we return to this component as an AuthScreen */ @@ -36,6 +38,11 @@ function LogOutPreviousUserPage({session, route}: LogOutPreviousUserPageProps) { if (isSupportalLogin) { SessionActions.signInWithSupportAuthToken(route.params.shortLivedAuthToken ?? ''); + Navigation.isNavigationReady().then(() => { + // We must call goBack() to remove the /transition route from history + Navigation.goBack(); + Navigation.navigate(ROUTES.HOME); + }); return; } From 907b2e438fa1f4ad988f9900ffe9b7cb425ee3d1 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 4 Mar 2024 15:59:32 +0100 Subject: [PATCH 57/72] removing transition from route on loginWithShortLivedAuthToken --- src/pages/LogInWithShortLivedAuthTokenPage.tsx | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/pages/LogInWithShortLivedAuthTokenPage.tsx b/src/pages/LogInWithShortLivedAuthTokenPage.tsx index 08804d3aa8e5..05402b1fd0d6 100644 --- a/src/pages/LogInWithShortLivedAuthTokenPage.tsx +++ b/src/pages/LogInWithShortLivedAuthTokenPage.tsx @@ -20,6 +20,7 @@ import ONYXKEYS from '@src/ONYXKEYS'; import type SCREENS from '@src/SCREENS'; import type {Account} from '@src/types/onyx'; import CONST from '@src/CONST'; +import ROUTES from '@src/ROUTES'; type LogInWithShortLivedAuthTokenPageOnyxProps = { /** The details about the account that the user is signing in with */ @@ -40,6 +41,11 @@ function LogInWithShortLivedAuthTokenPage({route, account}: LogInWithShortLivedA if (!account?.isLoading && authTokenType === CONST.AUTH_TOKEN_TYPES.SUPPORT) { Session.signInWithSupportAuthToken(shortLivedAuthToken); + Navigation.isNavigationReady().then(() => { + // We must call goBack() to remove the /transition route from history + Navigation.goBack(); + Navigation.navigate(ROUTES.HOME); + }); return; } From 25021a36b7239b48e7693ef36f644819531524a3 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 4 Mar 2024 16:00:44 +0100 Subject: [PATCH 58/72] add additional logic to Session to confirm we're stashing credentials correctly --- src/libs/actions/Session/index.ts | 77 ++++++++++++++++--------------- 1 file changed, 39 insertions(+), 38 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index df3abd54e8cd..16f8170d6e71 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -169,23 +169,16 @@ function signInWithSupportAuthToken(authToken: string) { */ function signOut() { Log.info('Flushing logs before signing out', true, {}, true); + const params = { + // Send current authToken because we will immediately clear it once triggering this command + authToken: NetworkStore.getAuthToken(), + partnerUserID: credentials?.autoGeneratedLogin ?? '', + partnerName: CONFIG.EXPENSIFY.PARTNER_NAME, + partnerPassword: CONFIG.EXPENSIFY.PARTNER_PASSWORD, + shouldRetry: false, + }; - // In case this is a supportal token, we won't have infinite sessions setup since the token will be - // short lived. So we can just remove the token and we can just skip calling logout. - if (isSupportalToken()) { - clearSupportAuthToken(); - } else { - const params = { - // Send current authToken because we will immediately clear it once triggering this command - authToken: NetworkStore.getAuthToken(), - partnerUserID: credentials?.autoGeneratedLogin ?? '', - partnerName: CONFIG.EXPENSIFY.PARTNER_NAME, - partnerPassword: CONFIG.EXPENSIFY.PARTNER_PASSWORD, - shouldRetry: false, - }; - - API.write(WRITE_COMMANDS.LOG_OUT, params); - } + API.write(WRITE_COMMANDS.LOG_OUT, params); clearCache().then(() => { Log.info('Cleared all cache data', true, {}, true); @@ -207,36 +200,44 @@ function hasStashedSession(): boolean { function signOutAndRedirectToSignIn(shouldReplaceCurrentScreen?: boolean, stashSession?: boolean) { Log.info('Redirecting to Sign In because signOut() was called'); hideContextMenu(false); - if (!isAnonymousUser()) { - // If we are not currently using a support token, and we received stashSession as true, we need to - // store the credentials so the user doesn't need to login again after they finish their supportal - // action. This needs to happen before we call `redirectToSignIn` - const isSupportal = isSupportalToken(); - if(!isSupportal && stashSession) { - Onyx.multiSet({ - [ONYXKEYS.STASHED_CREDENTIALS]: credentials, - [ONYXKEYS.STASHED_SESSION]: session, - }); - } // We'll only call signOut if we're not stashing the session, otherwise we'll call the API to invalidate // the autogenerated credentials used for infinite sessions - if (!stashSession) { + const isSupportal = isSupportalToken(); + if (!isSupportal && !stashSession) { signOut(); } - // This function will clear the whole storage + // If we are not currently using a support token, and we received stashSession as true, we need to + // store the credentials so the user doesn't need to login again after they finish their supportal + // action. This needs to computed before we call `redirectToSignIn` + const shouldStashSession = !isSupportal && stashSession; + + // Otherwise, if this is a supportal access and we have a stashed session, then we need to restore + // the stashed session instead instead of completely logging out + const shouldRestoreStashedSession = isSupportal && hasStashedSession(); + + // The function redirectToSignIn will clear the whole storage, so let's keep a copy of the current + // credentials and session, as well as the stashed version of it, so we can set them as the current + // if needed below values after clearing the storage + const credentialsCopy = credentials; + const sessionCopy = session; + const stashedCredentialsCopy = stashedCredentials; + const stashedSessionCopy = stashedSession; let redirectPromise = redirectToSignIn(); - - // If someone was logged as a supportal user, let's check if there's any credentials stashed, and - // if there is, let's set that session as the main one instead of completely logging out. - if(isSupportal && hasStashedSession()) { + if(shouldStashSession) { + redirectPromise.then(() => { + Onyx.multiSet({ + [ONYXKEYS.STASHED_CREDENTIALS]: credentialsCopy, + [ONYXKEYS.STASHED_SESSION]: sessionCopy, + }); + }); + } + if(shouldRestoreStashedSession) { + debugger; redirectPromise.then(() => { Onyx.multiSet({ - [ONYXKEYS.CREDENTIALS]: stashedCredentials, - [ONYXKEYS.SESSION]: stashedSession, - // Let's also remove the stashed session/credentials, since it's not needed anymore - [ONYXKEYS.STASHED_CREDENTIALS]: null, - [ONYXKEYS.STASHED_SESSION]: null, + [ONYXKEYS.CREDENTIALS]: stashedCredentialsCopy, + [ONYXKEYS.SESSION]: stashedSessionCopy, }); }); } From 61ec184135a71645ee9bad08b06d13ff1bbd169e Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 4 Mar 2024 16:01:16 +0100 Subject: [PATCH 59/72] Add propertitieson authscreen to guarantee we're executing the right logic --- src/libs/Navigation/AppNavigator/AuthScreens.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.tsx b/src/libs/Navigation/AppNavigator/AuthScreens.tsx index 00c96d436496..c8d7d453525c 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.tsx +++ b/src/libs/Navigation/AppNavigator/AuthScreens.tsx @@ -170,8 +170,9 @@ function AuthScreens({session, lastOpenedPublicRoomID, isUsingMemoryOnlyKeys = f const shouldGetAllData = !!isUsingMemoryOnlyKeys || SessionUtils.didUserLogInDuringSession(); // Sign out the current user if we're transitioning with a different user const isTransitioning = currentUrl.includes(ROUTES.TRANSITION_BETWEEN_APPS); + const isSupportalTransition = currentUrl.includes('authTokenType=support'); if (isLoggingInAsNewUser && isTransitioning) { - Session.signOutAndRedirectToSignIn(); + Session.signOutAndRedirectToSignIn(false, isSupportalTransition); return; } From ffb1d56549c6278892d1e97a10fe856ab54044a2 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 4 Mar 2024 16:04:03 +0100 Subject: [PATCH 60/72] adding spanish translation --- src/languages/es.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/languages/es.ts b/src/languages/es.ts index b3a8eef73d7c..02655faf00c7 100644 --- a/src/languages/es.ts +++ b/src/languages/es.ts @@ -856,6 +856,7 @@ export default { logSizeTooLarge: ({size}: LogSizeParams) => `El tamaño del registro excede el límite de ${size} MB. Utilice "Guardar registro" para descargar el archivo de registro.`, }, security: 'Seguridad', + restoreStashed: 'Restablecer login guardado', signOut: 'Desconectar', signOutConfirmationText: 'Si cierras sesión perderás los cambios hechos mientras estabas desconectado', versionLetter: 'v', From eed0aaf1ebe20b9d24ced6d0556c971a052ab7e0 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 4 Mar 2024 16:04:33 +0100 Subject: [PATCH 61/72] prettier --- src/libs/Network/NetworkStore.ts | 5 ++--- src/libs/actions/Session/index.ts | 10 +++++----- src/libs/actions/SignInRedirect.ts | 4 ++-- src/pages/LogInWithShortLivedAuthTokenPage.tsx | 4 ++-- src/pages/LogOutPreviousUserPage.tsx | 6 +++--- 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/libs/Network/NetworkStore.ts b/src/libs/Network/NetworkStore.ts index e9c5d7766d87..086beddc970e 100644 --- a/src/libs/Network/NetworkStore.ts +++ b/src/libs/Network/NetworkStore.ts @@ -1,10 +1,9 @@ import Onyx from 'react-native-onyx'; -import {SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from '@libs/API/types'; import type {ValueOf} from 'type-fest'; +import {SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from '@libs/API/types'; +import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type Credentials from '@src/types/onyx/Credentials'; -import CONST from '@src/CONST'; - let credentials: Credentials | null = null; let authToken: string | null = null; diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 16f8170d6e71..081987a84793 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -48,8 +48,8 @@ import ROUTES from '@src/ROUTES'; import SCREENS from '@src/SCREENS'; import type Credentials from '@src/types/onyx/Credentials'; import type {AutoAuthState} from '@src/types/onyx/Session'; -import clearCache from './clearCache'; import Session from '@src/types/onyx/Session'; +import clearCache from './clearCache'; let session: Session = {}; let authPromiseResolver: ((value: boolean) => void) | null = null; @@ -215,16 +215,16 @@ function signOutAndRedirectToSignIn(shouldReplaceCurrentScreen?: boolean, stashS // Otherwise, if this is a supportal access and we have a stashed session, then we need to restore // the stashed session instead instead of completely logging out const shouldRestoreStashedSession = isSupportal && hasStashedSession(); - + // The function redirectToSignIn will clear the whole storage, so let's keep a copy of the current - // credentials and session, as well as the stashed version of it, so we can set them as the current + // credentials and session, as well as the stashed version of it, so we can set them as the current // if needed below values after clearing the storage const credentialsCopy = credentials; const sessionCopy = session; const stashedCredentialsCopy = stashedCredentials; const stashedSessionCopy = stashedSession; let redirectPromise = redirectToSignIn(); - if(shouldStashSession) { + if (shouldStashSession) { redirectPromise.then(() => { Onyx.multiSet({ [ONYXKEYS.STASHED_CREDENTIALS]: credentialsCopy, @@ -232,7 +232,7 @@ function signOutAndRedirectToSignIn(shouldReplaceCurrentScreen?: boolean, stashS }); }); } - if(shouldRestoreStashedSession) { + if (shouldRestoreStashedSession) { debugger; redirectPromise.then(() => { Onyx.multiSet({ diff --git a/src/libs/actions/SignInRedirect.ts b/src/libs/actions/SignInRedirect.ts index 39c98522baa2..c6d4ffcaa30f 100644 --- a/src/libs/actions/SignInRedirect.ts +++ b/src/libs/actions/SignInRedirect.ts @@ -13,7 +13,7 @@ Onyx.connect({ }, }); -function clearStorageAndRedirect(errorMessage?: string) : Promise { +function clearStorageAndRedirect(errorMessage?: string): Promise { // Under certain conditions, there are key-values we'd like to keep in storage even when a user is logged out. // We pass these into the clear() method in order to avoid having to reset them on a delayed tick and getting // flashes of unwanted default state. @@ -46,7 +46,7 @@ function clearStorageAndRedirect(errorMessage?: string) : Promise { * * @param [errorMessage] error message to be displayed on the sign in page */ -function redirectToSignIn(errorMessage?: string) : Promise { +function redirectToSignIn(errorMessage?: string): Promise { return clearStorageAndRedirect(errorMessage); } diff --git a/src/pages/LogInWithShortLivedAuthTokenPage.tsx b/src/pages/LogInWithShortLivedAuthTokenPage.tsx index 05402b1fd0d6..d002b87b15fa 100644 --- a/src/pages/LogInWithShortLivedAuthTokenPage.tsx +++ b/src/pages/LogInWithShortLivedAuthTokenPage.tsx @@ -16,11 +16,11 @@ import Log from '@libs/Log'; import Navigation from '@libs/Navigation/Navigation'; import type {PublicScreensParamList} from '@libs/Navigation/types'; import * as Session from '@userActions/Session'; +import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; +import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; import type {Account} from '@src/types/onyx'; -import CONST from '@src/CONST'; -import ROUTES from '@src/ROUTES'; type LogInWithShortLivedAuthTokenPageOnyxProps = { /** The details about the account that the user is signing in with */ diff --git a/src/pages/LogOutPreviousUserPage.tsx b/src/pages/LogOutPreviousUserPage.tsx index 1bd28824dc06..3df7a4cdb9f3 100644 --- a/src/pages/LogOutPreviousUserPage.tsx +++ b/src/pages/LogOutPreviousUserPage.tsx @@ -4,15 +4,15 @@ import {Linking} from 'react-native'; import {withOnyx} from 'react-native-onyx'; import type {OnyxEntry} from 'react-native-onyx'; import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator'; +import Navigation from '@libs/Navigation/Navigation'; import * as SessionUtils from '@libs/SessionUtils'; import type {AuthScreensParamList} from '@navigation/types'; import * as SessionActions from '@userActions/Session'; +import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; +import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; import type {Session} from '@src/types/onyx'; -import CONST from '@src/CONST'; -import Navigation from '@libs/Navigation/Navigation'; -import ROUTES from '@src/ROUTES'; type LogOutPreviousUserPageOnyxProps = { /** The data about the current session which will be set once the user is authenticated and we return to this component as an AuthScreen */ From f06e89023134285b0b27b35996dd24fa66b49c59 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 4 Mar 2024 16:15:50 +0100 Subject: [PATCH 62/72] linter --- src/libs/actions/Session/index.ts | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index f3d2c448fb80..dd05527e8419 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -107,13 +107,6 @@ function setSupportAuthToken(supportAuthToken: string, email: string, accountID: Onyx.set(ONYXKEYS.LAST_VISITED_PATH, ''); } -function clearSupportAuthToken() { - Onyx.set(ONYXKEYS.SESSION, null).then(() => { - Log.info('[Supportal] Authtoken removed'); - }); - Onyx.set(ONYXKEYS.LAST_VISITED_PATH, null); -} - function getShortLivedLoginParams() { const optimisticData: OnyxUpdate[] = [ { @@ -223,7 +216,7 @@ function signOutAndRedirectToSignIn(shouldReplaceCurrentScreen?: boolean, stashS const sessionCopy = session; const stashedCredentialsCopy = stashedCredentials; const stashedSessionCopy = stashedSession; - let redirectPromise = redirectToSignIn(); + const redirectPromise = redirectToSignIn(); if (shouldStashSession) { redirectPromise.then(() => { Onyx.multiSet({ @@ -233,7 +226,6 @@ function signOutAndRedirectToSignIn(shouldReplaceCurrentScreen?: boolean, stashS }); } if (shouldRestoreStashedSession) { - debugger; redirectPromise.then(() => { Onyx.multiSet({ [ONYXKEYS.CREDENTIALS]: stashedCredentialsCopy, From 573bbb5dcb5c29bec84854501541536c2baecdb0 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 4 Mar 2024 16:23:47 +0100 Subject: [PATCH 63/72] setting settion import as type --- src/libs/actions/Session/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index dd05527e8419..4af32d10f94b 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -48,7 +48,7 @@ import ROUTES from '@src/ROUTES'; import SCREENS from '@src/SCREENS'; import type Credentials from '@src/types/onyx/Credentials'; import type {AutoAuthState} from '@src/types/onyx/Session'; -import Session from '@src/types/onyx/Session'; +import type Session from '@src/types/onyx/Session'; import clearCache from './clearCache'; let session: Session = {}; From 6d639410d9013e3dddd091de3d0963d8f00fa56a Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 4 Mar 2024 16:55:28 +0100 Subject: [PATCH 64/72] refactor function for simplicity --- src/libs/actions/Session/index.ts | 63 ++++++++++++++++--------------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 4af32d10f94b..586253639733 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -194,45 +194,48 @@ function signOutAndRedirectToSignIn(shouldReplaceCurrentScreen?: boolean, stashS Log.info('Redirecting to Sign In because signOut() was called'); hideContextMenu(false); if (!isAnonymousUser()) { - // We'll only call signOut if we're not stashing the session, otherwise we'll call the API to invalidate - // the autogenerated credentials used for infinite sessions + // We'll only call signOut if we're not stashing the session and this is not a supportal session, + // otherwise we'll call the API to invalidate the autogenerated credentials used for infinite + // session. SignOut also clears the storage, so we can just return after calling it. const isSupportal = isSupportalToken(); if (!isSupportal && !stashSession) { signOut(); + return; } + + // The function redirectToSignIn will clear the whole storage, so let's create our onyx params + // updates for the credentials before we call it + let onyxSetParams = {}; + // If we are not currently using a support token, and we received stashSession as true, we need to // store the credentials so the user doesn't need to login again after they finish their supportal // action. This needs to computed before we call `redirectToSignIn` - const shouldStashSession = !isSupportal && stashSession; - - // Otherwise, if this is a supportal access and we have a stashed session, then we need to restore - // the stashed session instead instead of completely logging out - const shouldRestoreStashedSession = isSupportal && hasStashedSession(); - - // The function redirectToSignIn will clear the whole storage, so let's keep a copy of the current - // credentials and session, as well as the stashed version of it, so we can set them as the current - // if needed below values after clearing the storage - const credentialsCopy = credentials; - const sessionCopy = session; - const stashedCredentialsCopy = stashedCredentials; - const stashedSessionCopy = stashedSession; - const redirectPromise = redirectToSignIn(); - if (shouldStashSession) { - redirectPromise.then(() => { - Onyx.multiSet({ - [ONYXKEYS.STASHED_CREDENTIALS]: credentialsCopy, - [ONYXKEYS.STASHED_SESSION]: sessionCopy, - }); - }); + if (!isSupportal && stashSession) { + onyxSetParams = { + [ONYXKEYS.STASHED_CREDENTIALS]: credentials, + [ONYXKEYS.STASHED_SESSION]: session, + }; } - if (shouldRestoreStashedSession) { - redirectPromise.then(() => { - Onyx.multiSet({ - [ONYXKEYS.CREDENTIALS]: stashedCredentialsCopy, - [ONYXKEYS.SESSION]: stashedSessionCopy, - }); - }); + // If this is a supportal token, and we've received the parameters to stashSession as true, and + // we already have a stashedSession, that means we are supportaled, currently supportaling + // into another account and we want to keep the stashed data from the original account. + if (isSupportal && stashSession && hasStashedSession()) { + onyxSetParams = { + [ONYXKEYS.STASHED_CREDENTIALS]: stashedCredentials, + [ONYXKEYS.STASHED_SESSION]: stashedSession, + }; + } + // Now if this is a supportal access, we do not want to stash the current session and we have a + // stashed session, then we need to restore the stashed session instead of completely logging out + if (isSupportal && !stashSession && hasStashedSession()) { + onyxSetParams = { + [ONYXKEYS.CREDENTIALS]: stashedCredentials, + [ONYXKEYS.SESSION]: stashedSession, + }; } + redirectToSignIn().then(() => { + Onyx.multiSet(onyxSetParams); + }); } else { if (Navigation.isActiveRoute(ROUTES.SIGN_IN_MODAL)) { return; From a5f45b2b13addf594bca22571fd36cd9e5665356 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 4 Mar 2024 16:56:00 +0100 Subject: [PATCH 65/72] prettier again --- src/libs/actions/Session/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 586253639733..59ff1d6c95bf 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -194,8 +194,8 @@ function signOutAndRedirectToSignIn(shouldReplaceCurrentScreen?: boolean, stashS Log.info('Redirecting to Sign In because signOut() was called'); hideContextMenu(false); if (!isAnonymousUser()) { - // We'll only call signOut if we're not stashing the session and this is not a supportal session, - // otherwise we'll call the API to invalidate the autogenerated credentials used for infinite + // We'll only call signOut if we're not stashing the session and this is not a supportal session, + // otherwise we'll call the API to invalidate the autogenerated credentials used for infinite // session. SignOut also clears the storage, so we can just return after calling it. const isSupportal = isSupportalToken(); if (!isSupportal && !stashSession) { @@ -217,7 +217,7 @@ function signOutAndRedirectToSignIn(shouldReplaceCurrentScreen?: boolean, stashS }; } // If this is a supportal token, and we've received the parameters to stashSession as true, and - // we already have a stashedSession, that means we are supportaled, currently supportaling + // we already have a stashedSession, that means we are supportaled, currently supportaling // into another account and we want to keep the stashed data from the original account. if (isSupportal && stashSession && hasStashedSession()) { onyxSetParams = { @@ -225,7 +225,7 @@ function signOutAndRedirectToSignIn(shouldReplaceCurrentScreen?: boolean, stashS [ONYXKEYS.STASHED_SESSION]: stashedSession, }; } - // Now if this is a supportal access, we do not want to stash the current session and we have a + // Now if this is a supportal access, we do not want to stash the current session and we have a // stashed session, then we need to restore the stashed session instead of completely logging out if (isSupportal && !stashSession && hasStashedSession()) { onyxSetParams = { From 114aee37a53b96964049e07bf226490c69c59484 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 4 Mar 2024 23:22:44 +0100 Subject: [PATCH 66/72] renaming property to clearer name --- src/libs/actions/Session/index.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 59ff1d6c95bf..c25c78639867 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -190,7 +190,7 @@ function hasStashedSession(): boolean { return Boolean(stashedSession.authToken && stashedCredentials.autoGeneratedLogin && stashedCredentials.autoGeneratedLogin !== ''); } -function signOutAndRedirectToSignIn(shouldReplaceCurrentScreen?: boolean, stashSession?: boolean) { +function signOutAndRedirectToSignIn(shouldReplaceCurrentScreen?: boolean, shouldStashSession?: boolean) { Log.info('Redirecting to Sign In because signOut() was called'); hideContextMenu(false); if (!isAnonymousUser()) { @@ -198,7 +198,7 @@ function signOutAndRedirectToSignIn(shouldReplaceCurrentScreen?: boolean, stashS // otherwise we'll call the API to invalidate the autogenerated credentials used for infinite // session. SignOut also clears the storage, so we can just return after calling it. const isSupportal = isSupportalToken(); - if (!isSupportal && !stashSession) { + if (!isSupportal && !shouldStashSession) { signOut(); return; } @@ -210,7 +210,7 @@ function signOutAndRedirectToSignIn(shouldReplaceCurrentScreen?: boolean, stashS // If we are not currently using a support token, and we received stashSession as true, we need to // store the credentials so the user doesn't need to login again after they finish their supportal // action. This needs to computed before we call `redirectToSignIn` - if (!isSupportal && stashSession) { + if (!isSupportal && shouldStashSession) { onyxSetParams = { [ONYXKEYS.STASHED_CREDENTIALS]: credentials, [ONYXKEYS.STASHED_SESSION]: session, @@ -219,7 +219,7 @@ function signOutAndRedirectToSignIn(shouldReplaceCurrentScreen?: boolean, stashS // If this is a supportal token, and we've received the parameters to stashSession as true, and // we already have a stashedSession, that means we are supportaled, currently supportaling // into another account and we want to keep the stashed data from the original account. - if (isSupportal && stashSession && hasStashedSession()) { + if (isSupportal && shouldStashSession && hasStashedSession()) { onyxSetParams = { [ONYXKEYS.STASHED_CREDENTIALS]: stashedCredentials, [ONYXKEYS.STASHED_SESSION]: stashedSession, @@ -227,7 +227,7 @@ function signOutAndRedirectToSignIn(shouldReplaceCurrentScreen?: boolean, stashS } // Now if this is a supportal access, we do not want to stash the current session and we have a // stashed session, then we need to restore the stashed session instead of completely logging out - if (isSupportal && !stashSession && hasStashedSession()) { + if (isSupportal && !shouldStashSession && hasStashedSession()) { onyxSetParams = { [ONYXKEYS.CREDENTIALS]: stashedCredentials, [ONYXKEYS.SESSION]: stashedSession, From 4af67a976ab57846d5fc36d80f84f8e6043ece6a Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 4 Mar 2024 23:27:49 +0100 Subject: [PATCH 67/72] removing check if it's supportal call when enhancing the parameters --- src/libs/Network/enhanceParameters.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/Network/enhanceParameters.ts b/src/libs/Network/enhanceParameters.ts index 5aac99aafd41..b8b8993a52b0 100644 --- a/src/libs/Network/enhanceParameters.ts +++ b/src/libs/Network/enhanceParameters.ts @@ -16,7 +16,7 @@ function isAuthTokenRequired(command: string): boolean { export default function enhanceParameters(command: string, parameters: Record): Record { const finalParameters = {...parameters}; - if (isAuthTokenRequired(command) && ((NetworkStore.isSupportAuthToken() && NetworkStore.isSupportRequest(command)) || !parameters.authToken)) { + if (isAuthTokenRequired(command) && !parameters.authToken) { finalParameters.authToken = NetworkStore.getAuthToken(); } From 20b943daa034accabf68a21a86454542b629ebbd Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 4 Mar 2024 23:30:49 +0100 Subject: [PATCH 68/72] setting parameters as optional --- src/libs/Navigation/types.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/Navigation/types.ts b/src/libs/Navigation/types.ts index 4860a7475ed6..3b19471b2001 100644 --- a/src/libs/Navigation/types.ts +++ b/src/libs/Navigation/types.ts @@ -518,8 +518,8 @@ type BottomTabNavigatorParamList = { type SharedScreensParamList = { [NAVIGATORS.BOTTOM_TAB_NAVIGATOR]: NavigatorScreenParams; [SCREENS.TRANSITION_BETWEEN_APPS]: { - email: string; - accountID: number; + email?: string; + accountID?: number; error?: string; shortLivedAuthToken?: string; shortLivedToken?: string; From 6fd8978da568adee87dc38a05b4fce49e5c9d84d Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 4 Mar 2024 23:35:42 +0100 Subject: [PATCH 69/72] DRYing functions --- src/libs/Network/NetworkStore.ts | 5 ----- src/libs/Request.ts | 3 ++- src/libs/actions/Session/index.ts | 6 +++--- src/pages/settings/InitialSettingsPage.tsx | 2 +- 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/libs/Network/NetworkStore.ts b/src/libs/Network/NetworkStore.ts index 086beddc970e..4104b1755222 100644 --- a/src/libs/Network/NetworkStore.ts +++ b/src/libs/Network/NetworkStore.ts @@ -101,10 +101,6 @@ function isSupportRequest(command: string): boolean { return [WRITE_COMMANDS.OPEN_APP, SIDE_EFFECT_REQUEST_COMMANDS.RECONNECT_APP, SIDE_EFFECT_REQUEST_COMMANDS.OPEN_REPORT].some((cmd) => cmd === command); } -function isSupportAuthToken(): boolean { - return authTokenType === CONST.AUTH_TOKEN_TYPES.SUPPORT; -} - function setAuthToken(newAuthToken: string | null) { authToken = newAuthToken; } @@ -137,6 +133,5 @@ export { setIsAuthenticating, getCredentials, checkRequiredData, - isSupportAuthToken, isSupportRequest, }; diff --git a/src/libs/Request.ts b/src/libs/Request.ts index 8d51486efad1..0d5d6e2e3efa 100644 --- a/src/libs/Request.ts +++ b/src/libs/Request.ts @@ -4,6 +4,7 @@ import HttpUtils from './HttpUtils'; import type Middleware from './Middleware/types'; import enhanceParameters from './Network/enhanceParameters'; import * as NetworkStore from './Network/NetworkStore'; +import { isSupportalToken } from './actions/Session'; let middlewares: Middleware[] = []; @@ -12,7 +13,7 @@ function makeXHR(request: Request): Promise { return NetworkStore.hasReadRequiredDataFromStorage().then((): Promise => { // If we're using the Supportal token and this is not a Supportal request // let's just return a promise that will resolve itself. - if (NetworkStore.isSupportAuthToken() && !NetworkStore.isSupportRequest(request.command)) { + if (isSupportalToken() && !NetworkStore.isSupportRequest(request.command)) { return new Promise((resolve) => { resolve(); }); diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index c25c78639867..01ac1a24e10a 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -88,7 +88,7 @@ Onyx.connect({ callback: (val) => (preferredLocale = val), }); -function isSupportalToken(): boolean { +function isSupportAuthToken(): boolean { return session.authTokenType === CONST.AUTH_TOKEN_TYPES.SUPPORT; } @@ -197,7 +197,7 @@ function signOutAndRedirectToSignIn(shouldReplaceCurrentScreen?: boolean, should // We'll only call signOut if we're not stashing the session and this is not a supportal session, // otherwise we'll call the API to invalidate the autogenerated credentials used for infinite // session. SignOut also clears the storage, so we can just return after calling it. - const isSupportal = isSupportalToken(); + const isSupportal = isSupportAuthToken(); if (!isSupportal && !shouldStashSession) { signOut(); return; @@ -991,6 +991,6 @@ export { waitForUserSignIn, canAccessRouteByAnonymousUser, signInWithSupportAuthToken, - isSupportalToken, + isSupportAuthToken, hasStashedSession, }; diff --git a/src/pages/settings/InitialSettingsPage.tsx b/src/pages/settings/InitialSettingsPage.tsx index 0adea503be67..b29fd600ae16 100755 --- a/src/pages/settings/InitialSettingsPage.tsx +++ b/src/pages/settings/InitialSettingsPage.tsx @@ -129,7 +129,7 @@ function InitialSettingsPage({session, userWallet, bankAccountList, fundList, wa const accountMenuItemsData: Menu = useMemo(() => { const profileBrickRoadIndicator = UserUtils.getLoginListBrickRoadIndicator(loginList); const paymentCardList = fundList; - const signOutTranslationKey = Session.isSupportalToken() && Session.hasStashedSession() ? 'initialSettingsPage.restoreStashed' : 'initialSettingsPage.signOut'; + const signOutTranslationKey = Session.isSupportAuthToken() && Session.hasStashedSession() ? 'initialSettingsPage.restoreStashed' : 'initialSettingsPage.signOut'; const defaultMenu: Menu = { sectionStyle: styles.accountSettingsSectionContainer, sectionTranslationKey: 'initialSettingsPage.account', From 992ed3a3f5c0acc7ee16e9170521bb203c43f4a2 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 4 Mar 2024 23:39:51 +0100 Subject: [PATCH 70/72] DRYing functions --- src/libs/Request.ts | 4 ++-- src/libs/actions/Session/index.ts | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/libs/Request.ts b/src/libs/Request.ts index 0d5d6e2e3efa..93c35530fadd 100644 --- a/src/libs/Request.ts +++ b/src/libs/Request.ts @@ -4,7 +4,7 @@ import HttpUtils from './HttpUtils'; import type Middleware from './Middleware/types'; import enhanceParameters from './Network/enhanceParameters'; import * as NetworkStore from './Network/NetworkStore'; -import { isSupportalToken } from './actions/Session'; +import { isSupportAuthToken } from './actions/Session'; let middlewares: Middleware[] = []; @@ -13,7 +13,7 @@ function makeXHR(request: Request): Promise { return NetworkStore.hasReadRequiredDataFromStorage().then((): Promise => { // If we're using the Supportal token and this is not a Supportal request // let's just return a promise that will resolve itself. - if (isSupportalToken() && !NetworkStore.isSupportRequest(request.command)) { + if (isSupportAuthToken() && !NetworkStore.isSupportRequest(request.command)) { return new Promise((resolve) => { resolve(); }); diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 01ac1a24e10a..e75dc53b3004 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -127,8 +127,7 @@ function getShortLivedLoginParams() { }, ]; - // Subsequently, we revert it back to the default value of 'signedInWithShortLivedAuthToken' in 'successData' or 'failureData' to ensure the user is logged out on refresh - // We are combining both success and failure data params into one const as they are identical + // Subsequently, we revert it back to the default value of 'signedInWithShortLivedAuthToken' in 'finallyData' to ensure the user is logged out on refresh const finallyData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, From 2931b5e789fca93c9941963477b2447009405b8c Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Mon, 4 Mar 2024 23:42:32 +0100 Subject: [PATCH 71/72] getting function back to network --- src/libs/Network/NetworkStore.ts | 5 +++++ src/libs/Request.ts | 3 +-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/libs/Network/NetworkStore.ts b/src/libs/Network/NetworkStore.ts index 4104b1755222..086beddc970e 100644 --- a/src/libs/Network/NetworkStore.ts +++ b/src/libs/Network/NetworkStore.ts @@ -101,6 +101,10 @@ function isSupportRequest(command: string): boolean { return [WRITE_COMMANDS.OPEN_APP, SIDE_EFFECT_REQUEST_COMMANDS.RECONNECT_APP, SIDE_EFFECT_REQUEST_COMMANDS.OPEN_REPORT].some((cmd) => cmd === command); } +function isSupportAuthToken(): boolean { + return authTokenType === CONST.AUTH_TOKEN_TYPES.SUPPORT; +} + function setAuthToken(newAuthToken: string | null) { authToken = newAuthToken; } @@ -133,5 +137,6 @@ export { setIsAuthenticating, getCredentials, checkRequiredData, + isSupportAuthToken, isSupportRequest, }; diff --git a/src/libs/Request.ts b/src/libs/Request.ts index 93c35530fadd..8d51486efad1 100644 --- a/src/libs/Request.ts +++ b/src/libs/Request.ts @@ -4,7 +4,6 @@ import HttpUtils from './HttpUtils'; import type Middleware from './Middleware/types'; import enhanceParameters from './Network/enhanceParameters'; import * as NetworkStore from './Network/NetworkStore'; -import { isSupportAuthToken } from './actions/Session'; let middlewares: Middleware[] = []; @@ -13,7 +12,7 @@ function makeXHR(request: Request): Promise { return NetworkStore.hasReadRequiredDataFromStorage().then((): Promise => { // If we're using the Supportal token and this is not a Supportal request // let's just return a promise that will resolve itself. - if (isSupportAuthToken() && !NetworkStore.isSupportRequest(request.command)) { + if (NetworkStore.isSupportAuthToken() && !NetworkStore.isSupportRequest(request.command)) { return new Promise((resolve) => { resolve(); }); From 205cb7f5b0ae72a81968965ba0fe4944b3ee5e05 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Tue, 5 Mar 2024 00:02:25 +0100 Subject: [PATCH 72/72] fix comment after merge --- src/libs/actions/Session/index.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index de478f489011..6a0f53c3d058 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -190,11 +190,10 @@ function signOutAndRedirectToSignIn(shouldReplaceCurrentScreen?: boolean, should if (!isAnonymousUser()) { // We'll only call signOut if we're not stashing the session and this is not a supportal session, // otherwise we'll call the API to invalidate the autogenerated credentials used for infinite - // session. SignOut also clears the storage, so we can just return after calling it. + // session. const isSupportal = isSupportAuthToken(); if (!isSupportal && !shouldStashSession) { signOut(); - return; } // The function redirectToSignIn will clear the whole storage, so let's create our onyx params