diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 91ad7b0d601b..046e4201c10d 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -106,7 +106,7 @@ Onyx.connectWithoutView({ }); let stashedSession: Session = {}; -Onyx.connect({ +Onyx.connectWithoutView({ key: ONYXKEYS.STASHED_SESSION, callback: (value) => (stashedSession = value ?? {}), }); @@ -277,8 +277,8 @@ function isAnonymousUser(sessionParam?: OnyxEntry): boolean { return (sessionParam?.authTokenType ?? session.authTokenType) === CONST.AUTH_TOKEN_TYPES.ANONYMOUS; } -function hasStashedSession(stashedCredentialsParam: Credentials | undefined): boolean { - return !!(stashedSession.authToken && stashedCredentialsParam?.autoGeneratedLogin && stashedCredentialsParam.autoGeneratedLogin !== ''); +function hasStashedSession(stashedSessionParam: Session | undefined, stashedCredentialsParam: Credentials | undefined): boolean { + return !!(stashedSessionParam?.authToken && stashedCredentialsParam?.autoGeneratedLogin && stashedCredentialsParam.autoGeneratedLogin !== ''); } /** @@ -367,7 +367,7 @@ function signOutAndRedirectToSignIn(shouldResetToHome?: boolean, shouldStashSess // 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 supportal-ed, currently supportal-ing // into another account and we want to keep the stashed data from the original account. - if (isSupportal && shouldStashSession && hasStashedSession(stashedCredentials)) { + if (isSupportal && shouldStashSession && hasStashedSession(stashedSession, stashedCredentials)) { onyxSetParams = { [ONYXKEYS.STASHED_CREDENTIALS]: stashedCredentials, [ONYXKEYS.STASHED_SESSION]: stashedSession, @@ -376,7 +376,7 @@ 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 (shouldRestoreStashedSession && !shouldStashSession && hasStashedSession(stashedCredentials)) { + if (shouldRestoreStashedSession && !shouldStashSession && hasStashedSession(stashedSession, stashedCredentials)) { if (CONFIG.IS_HYBRID_APP) { HybridAppModule.switchAccount({ newDotCurrentAccountEmail: stashedSession.email ?? '', @@ -392,7 +392,7 @@ function signOutAndRedirectToSignIn(shouldResetToHome?: boolean, shouldStashSess [ONYXKEYS.SESSION]: stashedSession, }; } - if (shouldRestoreStashedSession && !shouldStashSession && !hasStashedSession(stashedCredentials)) { + if (shouldRestoreStashedSession && !shouldStashSession && !hasStashedSession(stashedSession, stashedCredentials)) { Log.info('No stashed session found, clearing the session'); } @@ -419,7 +419,7 @@ function signOutAndRedirectToSignIn(shouldResetToHome?: boolean, shouldStashSess true, true, ); - } else if (isPerformingSupportalLogout && hasStashedSession(stashedCredentials)) { + } else if (isPerformingSupportalLogout && hasStashedSession(stashedSession, stashedCredentials)) { // We have confirmed here that the supportal agent was logged in, so we can restore the stashed session // and then redirect to the oldDot supportal page to restore the stashed session // Clear the Onyx DB of stale data that might be present from a previous session @@ -432,7 +432,7 @@ function signOutAndRedirectToSignIn(shouldResetToHome?: boolean, shouldStashSess }); }); }); - } else if (isPerformingSupportalLogout && !hasStashedSession(stashedCredentials)) { + } else if (isPerformingSupportalLogout && !hasStashedSession(stashedSession, stashedCredentials)) { // If the supportal agent was not logged in, we call `redirectToSignIn` to clear the Onyx DB // and then redirect to supportal and restore the stashed session redirectToSignIn().then(() => { @@ -443,7 +443,7 @@ function signOutAndRedirectToSignIn(shouldResetToHome?: boolean, shouldStashSess }); }); }); - } else if (shouldRestoreStashedSession && !shouldStashSession && hasStashedSession(stashedCredentials)) { + } else if (shouldRestoreStashedSession && !shouldStashSession && hasStashedSession(stashedSession, stashedCredentials)) { // Preserve SESSION during clear to avoid a login page flash, then restore the stashed session. Onyx.clear(KEYS_TO_PRESERVE_SUPPORTAL).then(() => { Onyx.multiSet(onyxSetParams).then(() => { diff --git a/src/pages/settings/InitialSettingsPage.tsx b/src/pages/settings/InitialSettingsPage.tsx index 4f04e97d3ec5..07d884810848 100755 --- a/src/pages/settings/InitialSettingsPage.tsx +++ b/src/pages/settings/InitialSettingsPage.tsx @@ -153,6 +153,7 @@ function InitialSettingsPage({currentUserPersonalDetails}: InitialSettingsPagePr const [lastDayFreeTrial] = useOnyx(ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL); const [unsharedBankAccount] = useOnyx(ONYXKEYS.UNSHARE_BANK_ACCOUNT); const [stashedCredentials] = useOnyx(ONYXKEYS.STASHED_CREDENTIALS); + const [stashedSession] = useOnyx(ONYXKEYS.STASHED_SESSION); const privateSubscription = usePrivateSubscription(); const subscriptionPlan = useSubscriptionPlan(); const previousUserPersonalDetails = usePrevious(currentUserPersonalDetails); @@ -350,7 +351,7 @@ function InitialSettingsPage({currentUserPersonalDetails}: InitialSettingsPagePr * Return a list of menu items data for general section * @returns object with translationKey, style and items for the general section */ - const signOutTranslationKey = isSupportAuthToken() && hasStashedSession(stashedCredentials) ? 'initialSettingsPage.restoreStashed' : 'initialSettingsPage.signOut'; + const signOutTranslationKey = isSupportAuthToken() && hasStashedSession(stashedSession, stashedCredentials) ? 'initialSettingsPage.restoreStashed' : 'initialSettingsPage.signOut'; const generalMenuItemsData: Menu = { sectionStyle: { ...styles.pt4, diff --git a/tests/actions/SessionTest.ts b/tests/actions/SessionTest.ts index b0a0a5eb0e00..039988390ffe 100644 --- a/tests/actions/SessionTest.ts +++ b/tests/actions/SessionTest.ts @@ -377,6 +377,56 @@ describe('Session', () => { }); }); + describe('hasStashedSession', () => { + // Clear any mocks from previous tests that might have mocked hasStashedSession + beforeEach(() => { + jest.restoreAllMocks(); + }); + + test('returns true when both stashedSession has authToken and stashedCredentials has valid autoGeneratedLogin', () => { + const stashedSession = {authToken: 'testAuthToken', email: 'test@expensify.com', accountID: 123, creationDate: new Date().getTime()}; + const stashedCredentials = {login: 'test@expensify.com', autoGeneratedLogin: 'autoLogin', autoGeneratedPassword: 'autoPassword'}; + + const result = SessionUtil.hasStashedSession(stashedSession, stashedCredentials); + + expect(result).toBe(true); + }); + + test('returns false when stashedSession authToken is missing', () => { + const stashedSession = {email: 'test@expensify.com', accountID: 123, creationDate: new Date().getTime()}; + const stashedCredentials = {login: 'test@expensify.com', autoGeneratedLogin: 'autoLogin', autoGeneratedPassword: 'autoPassword'}; + + const result = SessionUtil.hasStashedSession(stashedSession, stashedCredentials); + + expect(result).toBe(false); + }); + + test('returns false when autoGeneratedLogin is missing', () => { + const stashedSession = {authToken: 'testAuthToken', email: 'test@expensify.com', accountID: 123, creationDate: new Date().getTime()}; + const stashedCredentials = {login: 'test@expensify.com', autoGeneratedPassword: 'autoPassword'}; + + const result = SessionUtil.hasStashedSession(stashedSession, stashedCredentials); + + expect(result).toBe(false); + }); + + test('returns false when stashedSessionParam is undefined', () => { + const stashedCredentials = {login: 'test@expensify.com', autoGeneratedLogin: 'autoLogin', autoGeneratedPassword: 'autoPassword'}; + + const result = SessionUtil.hasStashedSession(undefined, stashedCredentials); + + expect(result).toBe(false); + }); + + test('returns false when stashedCredentialsParam is undefined', () => { + const stashedSession = {authToken: 'testAuthToken', email: 'test@expensify.com', accountID: 123, creationDate: new Date().getTime()}; + + const result = SessionUtil.hasStashedSession(stashedSession, undefined); + + expect(result).toBe(false); + }); + }); + describe('SAML sign out', () => { const mockedOpenAuthSessionAsync = jest.mocked(openAuthSessionAsync);