From 87dc9a07840d766fc198fcbdf97d6c24ed9cb77e Mon Sep 17 00:00:00 2001 From: Abdelhafidh Belalia <16493223+s77rt@users.noreply.github.com> Date: Sun, 16 Mar 2025 23:24:30 +0100 Subject: [PATCH 1/7] hybrid app switch account if we have a stashed session for use --- src/libs/Middleware/HandleDeletedAccount.ts | 2 +- src/libs/actions/Session/index.ts | 24 +++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/libs/Middleware/HandleDeletedAccount.ts b/src/libs/Middleware/HandleDeletedAccount.ts index ec5532baa230d..0a7be16a1b90e 100644 --- a/src/libs/Middleware/HandleDeletedAccount.ts +++ b/src/libs/Middleware/HandleDeletedAccount.ts @@ -12,7 +12,7 @@ const handleDeletedAccount: Middleware = (requestResponse) => if (response?.jsonCode !== 408 || !response?.message?.includes('The account you are trying to use is deleted.')) { return response; } - signOutAndRedirectToSignIn(true, false, true, true); + signOutAndRedirectToSignIn(true, false, false, true); }); export default handleDeletedAccount; diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index d193d786baeeb..31fc05899127b 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -60,6 +60,7 @@ import type Credentials from '@src/types/onyx/Credentials'; import type Response from '@src/types/onyx/Response'; import type Session from '@src/types/onyx/Session'; import type {AutoAuthState} from '@src/types/onyx/Session'; +import {getCurrentUserAccountID} from '../Report'; import clearCache from './clearCache'; import updateSessionAuthTokens from './updateSessionAuthTokens'; @@ -108,6 +109,14 @@ Onyx.connect({ callback: (val) => (preferredLocale = val ?? null), }); +let activePolicyID: OnyxEntry; +Onyx.connect({ + key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, + callback: (newActivePolicyID) => { + activePolicyID = newActivePolicyID; + }, +}); + function isSupportAuthToken(): boolean { return session.authTokenType === CONST.AUTH_TOKEN_TYPES.SUPPORT; } @@ -223,7 +232,7 @@ function isExpiredSession(sessionCreationDate: number): boolean { return new Date().getTime() - sessionCreationDate >= CONST.SESSION_EXPIRATION_TIME_MS; } -function signOutAndRedirectToSignIn(shouldResetToHome?: boolean, shouldStashSession?: boolean, killHybridApp = true, shouldForceUseStashedSession?: boolean) { +function signOutAndRedirectToSignIn(shouldResetToHome?: boolean, shouldStashSession?: boolean, shouldKillHybridApp = true, shouldForceUseStashedSession?: boolean) { Log.info('Redirecting to Sign In because signOut() was called'); hideContextMenu(false); @@ -244,10 +253,11 @@ function signOutAndRedirectToSignIn(shouldResetToHome?: boolean, shouldStashSess } // In the HybridApp, we want the Old Dot to handle the sign out process - if (CONFIG.IS_HYBRID_APP && killHybridApp) { + if (CONFIG.IS_HYBRID_APP && shouldKillHybridApp) { HybridAppModule.closeReactNativeApp({shouldSignOut: true, shouldSetNVP: false}); return; } + // 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. @@ -280,6 +290,16 @@ function signOutAndRedirectToSignIn(shouldResetToHome?: boolean, shouldStashSess // Now if this is a supportal access or force use stashed session, 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 || shouldForceUseStashedSession) && !shouldStashSession && hasStashedSession()) { + if (CONFIG.IS_HYBRID_APP) { + HybridAppModule.switchAccount({ + newDotCurrentAccountEmail: stashedSession.email ?? '', + authToken: stashedSession.authToken ?? '', + policyID: activePolicyID ?? '', + accountID: String(getCurrentUserAccountID()), + }); + return; + } + onyxSetParams = { [ONYXKEYS.CREDENTIALS]: stashedCredentials, [ONYXKEYS.SESSION]: stashedSession, From 70018fa57906f1f41238f3eded9c7dd7d35817d5 Mon Sep 17 00:00:00 2001 From: Abdelhafidh Belalia <16493223+s77rt@users.noreply.github.com> Date: Mon, 17 Mar 2025 22:20:48 +0100 Subject: [PATCH 2/7] fix lint and code readability --- src/libs/actions/Session/index.ts | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 31fc05899127b..b83d184e174ac 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -60,7 +60,6 @@ import type Credentials from '@src/types/onyx/Credentials'; import type Response from '@src/types/onyx/Response'; import type Session from '@src/types/onyx/Session'; import type {AutoAuthState} from '@src/types/onyx/Session'; -import {getCurrentUserAccountID} from '../Report'; import clearCache from './clearCache'; import updateSessionAuthTokens from './updateSessionAuthTokens'; @@ -289,17 +288,17 @@ function signOutAndRedirectToSignIn(shouldResetToHome?: boolean, shouldStashSess // Now if this is a supportal access or force use stashed session, 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 || shouldForceUseStashedSession) && !shouldStashSession && hasStashedSession()) { - if (CONFIG.IS_HYBRID_APP) { - HybridAppModule.switchAccount({ - newDotCurrentAccountEmail: stashedSession.email ?? '', - authToken: stashedSession.authToken ?? '', - policyID: activePolicyID ?? '', - accountID: String(getCurrentUserAccountID()), - }); - return; - } - + const shouldSwitchAccount = (isSupportal || shouldForceUseStashedSession) && !shouldStashSession && hasStashedSession(); + if (shouldSwitchAccount && CONFIG.IS_HYBRID_APP) { + HybridAppModule.switchAccount({ + newDotCurrentAccountEmail: stashedSession.email ?? '', + authToken: stashedSession.authToken ?? '', + policyID: activePolicyID ?? '', + accountID: String(session.accountID ?? ''), + }); + return; + } + if (shouldSwitchAccount) { onyxSetParams = { [ONYXKEYS.CREDENTIALS]: stashedCredentials, [ONYXKEYS.SESSION]: stashedSession, From 7ccd0ee58a0d849dca2337e039baa9f6121a21d5 Mon Sep 17 00:00:00 2001 From: Abdelhafidh Belalia <16493223+s77rt@users.noreply.github.com> Date: Tue, 18 Mar 2025 00:17:39 +0100 Subject: [PATCH 3/7] lint --- src/libs/actions/Session/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index b83d184e174ac..37f109a34c8dd 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -293,8 +293,9 @@ function signOutAndRedirectToSignIn(shouldResetToHome?: boolean, shouldStashSess HybridAppModule.switchAccount({ newDotCurrentAccountEmail: stashedSession.email ?? '', authToken: stashedSession.authToken ?? '', + // eslint-disable-next-line rulesdir/no-default-id-values policyID: activePolicyID ?? '', - accountID: String(session.accountID ?? ''), + accountID: session.accountID ? String(session.accountID) : '', }); return; } From a38ff0d5b238720c9e055b33987cbfd0dfb1deff Mon Sep 17 00:00:00 2001 From: Abdelhafidh Belalia <16493223+s77rt@users.noreply.github.com> Date: Tue, 18 Mar 2025 00:44:19 +0100 Subject: [PATCH 4/7] newline for clarity --- 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 37f109a34c8dd..5dafe591eddad 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -276,6 +276,7 @@ function signOutAndRedirectToSignIn(shouldResetToHome?: boolean, shouldStashSess [ONYXKEYS.STASHED_SESSION]: session, }; } + // 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. From f2a6be5b601537c9972f38f16790c98cf5fec63a Mon Sep 17 00:00:00 2001 From: Abdelhafidh Belalia <16493223+s77rt@users.noreply.github.com> Date: Wed, 19 Mar 2025 05:44:44 +0100 Subject: [PATCH 5/7] do not signout if we are restoring a stashed session --- src/libs/actions/Session/index.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 5dafe591eddad..1051fbf8238c4 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -257,11 +257,13 @@ function signOutAndRedirectToSignIn(shouldResetToHome?: boolean, shouldStashSess return; } - // We'll only call signOut if we're not stashing the session and this is not a supportal session, + const isSupportal = isSupportAuthToken(); + const shouldRestoreStashedSession = isSupportal || shouldForceUseStashedSession; + + // We'll only call signOut if we're not stashing the session and not restoring a stashed session, // otherwise we'll call the API to invalidate the autogenerated credentials used for infinite // session. - const isSupportal = isSupportAuthToken(); - const signOutPromise: Promise = !isSupportal && !shouldStashSession ? signOut() : Promise.resolve(); + const signOutPromise: Promise = !shouldRestoreStashedSession && !shouldStashSession ? signOut() : Promise.resolve(); // The function redirectToSignIn will clear the whole storage, so let's create our onyx params // updates for the credentials before we call it @@ -287,10 +289,9 @@ function signOutAndRedirectToSignIn(shouldResetToHome?: boolean, shouldStashSess }; } - // Now if this is a supportal access or force use stashed session, 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 - const shouldSwitchAccount = (isSupportal || shouldForceUseStashedSession) && !shouldStashSession && hasStashedSession(); - if (shouldSwitchAccount && CONFIG.IS_HYBRID_APP) { + // If we should restore the stashed session, and we do not want to stash the current session, and we have a + // stashed session, then switch the account instead of completely logging out. + if (CONFIG.IS_HYBRID_APP && shouldRestoreStashedSession && !shouldStashSession && hasStashedSession()) { HybridAppModule.switchAccount({ newDotCurrentAccountEmail: stashedSession.email ?? '', authToken: stashedSession.authToken ?? '', @@ -300,14 +301,14 @@ function signOutAndRedirectToSignIn(shouldResetToHome?: boolean, shouldStashSess }); return; } - if (shouldSwitchAccount) { + if (shouldRestoreStashedSession && !shouldStashSession && hasStashedSession()) { onyxSetParams = { [ONYXKEYS.CREDENTIALS]: stashedCredentials, [ONYXKEYS.SESSION]: stashedSession, }; } - if (isSupportal && !shouldStashSession && !hasStashedSession()) { - Log.info('No stashed session found for supportal access, clearing the session'); + if (shouldRestoreStashedSession && !shouldStashSession && !hasStashedSession()) { + Log.info('No stashed session found, clearing the session'); } // Wait for signOut (if called), then redirect and update Onyx. From 5dc42ae8fe74a109bebef52b06af2aeb1a4e04f6 Mon Sep 17 00:00:00 2001 From: Abdelhafidh Belalia <16493223+s77rt@users.noreply.github.com> Date: Fri, 4 Apr 2025 09:29:59 +0100 Subject: [PATCH 6/7] If OD is in copilot, stash the original account data --- src/libs/actions/Session/index.ts | 35 +++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 1051fbf8238c4..4d1e834143a9a 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -291,17 +291,17 @@ function signOutAndRedirectToSignIn(shouldResetToHome?: boolean, shouldStashSess // If we should restore the stashed session, and we do not want to stash the current session, and we have a // stashed session, then switch the account instead of completely logging out. - if (CONFIG.IS_HYBRID_APP && shouldRestoreStashedSession && !shouldStashSession && hasStashedSession()) { - HybridAppModule.switchAccount({ - newDotCurrentAccountEmail: stashedSession.email ?? '', - authToken: stashedSession.authToken ?? '', - // eslint-disable-next-line rulesdir/no-default-id-values - policyID: activePolicyID ?? '', - accountID: session.accountID ? String(session.accountID) : '', - }); - return; - } if (shouldRestoreStashedSession && !shouldStashSession && hasStashedSession()) { + if (CONFIG.IS_HYBRID_APP) { + HybridAppModule.switchAccount({ + newDotCurrentAccountEmail: stashedSession.email ?? '', + authToken: stashedSession.authToken ?? '', + // eslint-disable-next-line rulesdir/no-default-id-values + policyID: activePolicyID ?? '', + accountID: session.accountID ? String(session.accountID) : '', + }); + } + onyxSetParams = { [ONYXKEYS.CREDENTIALS]: stashedCredentials, [ONYXKEYS.SESSION]: stashedSession, @@ -565,6 +565,8 @@ type HybridAppSettings = { encryptedAuthToken: string; nudgeMigrationTimestamp?: string; oldDotOriginalAccountEmail?: string; + stashedAuthToken?: string; + stashedAccountID?: string; }; function signInAfterTransitionFromOldDot(hybridAppSettings: string) { @@ -581,6 +583,8 @@ function signInAfterTransitionFromOldDot(hybridAppSettings: string) { isSingleNewDotEntry, primaryLogin, oldDotOriginalAccountEmail, + stashedAuthToken, + stashedAccountID, } = JSON.parse(hybridAppSettings) as HybridAppSettings; const clearOnyxForNewAccount = () => { @@ -596,7 +600,15 @@ function signInAfterTransitionFromOldDot(hybridAppSettings: string) { // This section controls copilot changes const currentUserEmail = getCurrentUserEmail(); - // If ND and OD account are the same - do nothing + // If OD is in copilot, stash the original account data + if (oldDotOriginalAccountEmail && oldDotOriginalAccountEmail !== email) { + return Onyx.multiSet({ + [ONYXKEYS.STASHED_SESSION]: {email: oldDotOriginalAccountEmail, authToken: stashedAuthToken, accountID: Number(stashedAccountID)}, + [ONYXKEYS.STASHED_CREDENTIALS]: {autoGeneratedLogin, autoGeneratedPassword}, + }); + } + + // If OD and ND account are the same - do nothing if (email === currentUserEmail) { return; } @@ -613,6 +625,7 @@ function signInAfterTransitionFromOldDot(hybridAppSettings: string) { // If we're not logged in - set stashed data return Onyx.multiSet({ + [ONYXKEYS.STASHED_SESSION]: {email, authToken, encryptedAuthToken: decodeURIComponent(encryptedAuthToken), accountID: Number(accountID)}, [ONYXKEYS.STASHED_CREDENTIALS]: {autoGeneratedLogin, autoGeneratedPassword}, }); }) From 75e42153d8677755c2f9a80c0f457f22cd00f8cf Mon Sep 17 00:00:00 2001 From: Abdelhafidh Belalia <16493223+s77rt@users.noreply.github.com> Date: Fri, 4 Apr 2025 09:35:02 +0100 Subject: [PATCH 7/7] undo unnecessary change --- src/libs/actions/Session/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 1754bbedf0219..54e2f9875de96 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -634,7 +634,6 @@ function signInAfterTransitionFromOldDot(hybridAppSettings: string) { // If we're not logged in - set stashed data return Onyx.multiSet({ - [ONYXKEYS.STASHED_SESSION]: {email, authToken, encryptedAuthToken: decodeURIComponent(encryptedAuthToken), accountID: Number(accountID)}, [ONYXKEYS.STASHED_CREDENTIALS]: {autoGeneratedLogin, autoGeneratedPassword}, }); })