Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stash session and credentials during copilot access #52980

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/libs/actions/Delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import * as SequentialQueue from '@libs/Network/SequentialQueue';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Delegate, DelegatedAccess, DelegateRole} from '@src/types/onyx/Account';
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 {confirmReadyToOpenApp, openApp} from './App';
import updateSessionAuthTokens from './Session/updateSessionAuthTokens';
import updateSessionUser from './Session/updateSessionUser';
Expand All @@ -25,13 +27,38 @@ Onyx.connect({
},
});

let credentials: Credentials = {};
Onyx.connect({
key: ONYXKEYS.CREDENTIALS,
callback: (value) => (credentials = value ?? {}),
});

let stashedCredentials: Credentials = {};
Onyx.connect({
key: ONYXKEYS.STASHED_CREDENTIALS,
callback: (value) => (stashedCredentials = value ?? {}),
});

let session: Session = {};
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (value) => (session = value ?? {}),
});

let stashedSession: Session = {};
Onyx.connect({
key: ONYXKEYS.STASHED_SESSION,
callback: (value) => (stashedSession = value ?? {}),
});

const KEYS_TO_PRESERVE_DELEGATE_ACCESS = [
ONYXKEYS.NVP_TRY_FOCUS_MODE,
ONYXKEYS.PREFERRED_THEME,
ONYXKEYS.NVP_PREFERRED_LOCALE,
ONYXKEYS.SESSION,
ONYXKEYS.STASHED_SESSION,
ONYXKEYS.IS_LOADING_APP,
ONYXKEYS.CREDENTIALS,
ONYXKEYS.STASHED_CREDENTIALS,

// We need to preserve the sidebar loaded state since we never unrender the sidebar when connecting as a delegate
// This allows the report screen to load correctly when the delegate token expires and the delegate is returned to their original account.
Expand All @@ -43,6 +70,9 @@ function connect(email: string) {
return;
}

Onyx.set(ONYXKEYS.STASHED_CREDENTIALS, credentials);
Onyx.set(ONYXKEYS.STASHED_SESSION, session);

const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -172,6 +202,11 @@ function disconnect() {
updateSessionAuthTokens(response?.authToken, response?.encryptedAuthToken);

NetworkStore.setAuthToken(response?.authToken ?? null);

Onyx.set(ONYXKEYS.CREDENTIALS, stashedCredentials);
Onyx.set(ONYXKEYS.SESSION, stashedSession);
Onyx.set(ONYXKEYS.STASHED_CREDENTIALS, {});
Onyx.set(ONYXKEYS.STASHED_SESSION, {});
confirmReadyToOpenApp();
openApp();

Expand Down
Loading