diff --git a/src/Expensify.tsx b/src/Expensify.tsx index 048d686cf959..9ee57669ce18 100644 --- a/src/Expensify.tsx +++ b/src/Expensify.tsx @@ -109,6 +109,7 @@ function Expensify() { const [currentOnboardingCompanySize] = useOnyx(ONYXKEYS.ONBOARDING_COMPANY_SIZE, {canBeMissing: true}); const [onboardingInitialPath] = useOnyx(ONYXKEYS.ONBOARDING_LAST_VISITED_PATH, {canBeMissing: true}); const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {canBeMissing: false}); + const [stashedCredentials = CONST.EMPTY_OBJECT] = useOnyx(ONYXKEYS.STASHED_CREDENTIALS, {canBeMissing: true}); useDebugShortcut(); usePriorityMode(); @@ -280,8 +281,8 @@ function Expensify() { if (account?.delegatedAccess?.delegates?.some((d) => d.email === account?.delegatedAccess?.delegate)) { return; } - disconnect(); - }, [account?.delegatedAccess?.delegates, account?.delegatedAccess?.delegate]); + disconnect({stashedCredentials}); + }, [account?.delegatedAccess?.delegates, account?.delegatedAccess?.delegate, stashedCredentials]); // Display a blank page until the onyx migration completes if (!isOnyxMigrated) { diff --git a/src/components/AccountSwitcher.tsx b/src/components/AccountSwitcher.tsx index cd31d56302a7..fc8ada5e5e9e 100644 --- a/src/components/AccountSwitcher.tsx +++ b/src/components/AccountSwitcher.tsx @@ -48,6 +48,7 @@ function AccountSwitcher({isScreenFocused}: AccountSwitcherProps) { const [accountID] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: false, selector: accountIDSelector}); const [isDebugModeEnabled] = useOnyx(ONYXKEYS.IS_DEBUG_MODE_ENABLED, {canBeMissing: true}); const [credentials] = useOnyx(ONYXKEYS.CREDENTIALS, {canBeMissing: true}); + const [stashedCredentials = CONST.EMPTY_OBJECT] = useOnyx(ONYXKEYS.STASHED_CREDENTIALS, {canBeMissing: true}); const buttonRef = useRef(null); const {windowHeight} = useWindowDimensions(); @@ -140,7 +141,7 @@ function AccountSwitcher({isScreenFocused}: AccountSwitcherProps) { close(() => setShouldShowOfflineModal(true)); return; } - disconnect(); + disconnect({stashedCredentials}); }, }), currentUserMenuItem, diff --git a/src/libs/actions/Delegate.ts b/src/libs/actions/Delegate.ts index ab8a847615c1..530f789429c8 100644 --- a/src/libs/actions/Delegate.ts +++ b/src/libs/actions/Delegate.ts @@ -20,12 +20,6 @@ import {getCurrentUserAccountID} from './Report'; import updateSessionAuthTokens from './Session/updateSessionAuthTokens'; import updateSessionUser from './Session/updateSessionUser'; -let stashedCredentials: Credentials = {}; -Onyx.connect({ - key: ONYXKEYS.STASHED_CREDENTIALS, - callback: (value) => (stashedCredentials = value ?? {}), -}); - let session: Session = {}; Onyx.connect({ key: ONYXKEYS.SESSION, @@ -95,6 +89,12 @@ type WithOldDotFlag = { isFromOldDot?: boolean; }; +type WithStashedCredentials = { + stashedCredentials: Credentials | undefined; +}; + +type DisconnectParams = WithStashedCredentials; + // Clear delegator-level errors type ClearDelegatorErrorsParams = WithDelegatedAccess; @@ -227,7 +227,7 @@ function connect({email, delegatedAccess, credentials, isFromOldDot = false}: Co }); } -function disconnect() { +function disconnect({stashedCredentials}: DisconnectParams) { const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE,