Skip to content
Open
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
25 changes: 25 additions & 0 deletions src/libs/actions/SignInRedirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {clearAllPolicies} from './Policy/Policy';
let currentIsOffline: boolean | undefined;
let currentShouldForceOffline: boolean | undefined;
let currentIsUsingImportedState: boolean | undefined;
let currentSessionAuthToken: string | undefined;
let currentCredentialsValidateCode: string | undefined;

// We use connectWithoutView here because we only need to track network state for sign-in redirect logic, which is not connected to any changes on the UI layer
Onyx.connectWithoutView({
Expand All @@ -28,6 +30,20 @@ Onyx.connectWithoutView({
},
});

Onyx.connectWithoutView({
key: ONYXKEYS.SESSION,
callback: (session) => {
currentSessionAuthToken = session?.authToken;
},
});

Onyx.connectWithoutView({
key: ONYXKEYS.CREDENTIALS,
callback: (credentials) => {
currentCredentialsValidateCode = credentials?.validateCode;
},
});

function clearStorageAndRedirect(errorMessage?: string): Promise<void> {
// 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
Expand All @@ -54,6 +70,15 @@ function clearStorageAndRedirect(errorMessage?: string): Promise<void> {
keysToPreserve.push(ONYXKEYS.NETWORK);
}

// When the user is in the middle of a 2FA sign-in flow (they've entered their magic code but not yet completed
// 2FA), we want to preserve their credentials and account state so that after a page refresh they are still
// prompted to enter their 2FA code rather than being sent back to the initial sign-in page.
const isIncompleteSignIn = !currentSessionAuthToken && !!currentCredentialsValidateCode;
if (isIncompleteSignIn) {
keysToPreserve.push(ONYXKEYS.CREDENTIALS);
keysToPreserve.push(ONYXKEYS.ACCOUNT);
}

return Onyx.clear(keysToPreserve).then(() => {
if (CONFIG.IS_HYBRID_APP) {
resetSignInFlow();
Expand Down
Loading