Skip to content

Commit

Permalink
Merge pull request #46087 from rushatgabhane/2fa-fix-2
Browse files Browse the repository at this point in the history
  • Loading branch information
francoisl authored Jul 24, 2024
2 parents 2bb1d00 + 52d68a0 commit 1084706
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
32 changes: 20 additions & 12 deletions src/libs/actions/Session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,19 @@ function toggleTwoFactorAuth(enable: boolean) {
API.write(enable ? WRITE_COMMANDS.ENABLE_TWO_FACTOR_AUTH : WRITE_COMMANDS.DISABLE_TWO_FACTOR_AUTH, null, {optimisticData, successData, failureData});
}

function validateTwoFactorAuth(twoFactorAuthCode: string) {
function updateAuthTokenAndOpenApp(authToken?: string, encryptedAuthToken?: string) {
// Update authToken in Onyx and in our local variables so that API requests will use the new authToken
updateSessionAuthTokens(authToken, encryptedAuthToken);

// Note: It is important to manually set the authToken that is in the store here since
// reconnectApp will immediate post and use the local authToken. Onyx updates subscribers lately so it is not
// enough to do the updateSessionAuthTokens() call above.
NetworkStore.setAuthToken(authToken ?? null);

openApp();
}

function validateTwoFactorAuth(twoFactorAuthCode: string, shouldClearData: boolean) {
const optimisticData = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -950,18 +962,14 @@ function validateTwoFactorAuth(twoFactorAuthCode: string) {
return;
}

const keysToPreserveWithPrivatePersonalDetails = [...KEYS_TO_PRESERVE, ONYXKEYS.PRIVATE_PERSONAL_DETAILS];
Onyx.clear(keysToPreserveWithPrivatePersonalDetails).then(() => {
// Update authToken in Onyx and in our local variables so that API requests will use the new authToken
updateSessionAuthTokens(response.authToken, response.encryptedAuthToken);

// Note: It is important to manually set the authToken that is in the store here since
// reconnectApp will immediate post and use the local authToken. Onyx updates subscribers lately so it is not
// enough to do the updateSessionAuthTokens() call above.
NetworkStore.setAuthToken(response.authToken ?? null);
// Clear onyx data if the user has just signed in and is forced to add 2FA
if (shouldClearData) {
const keysToPreserveWithPrivatePersonalDetails = [...KEYS_TO_PRESERVE, ONYXKEYS.PRIVATE_PERSONAL_DETAILS];
Onyx.clear(keysToPreserveWithPrivatePersonalDetails).then(() => updateAuthTokenAndOpenApp(response.authToken, response.encryptedAuthToken));
return;
}

openApp();
});
updateAuthTokenAndOpenApp(response.authToken, response.encryptedAuthToken);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function BaseTwoFactorAuthForm({account, autoComplete}: BaseTwoFactorAuthFormPro
const [formError, setFormError] = useState<{twoFactorAuthCode?: string}>({});
const [twoFactorAuthCode, setTwoFactorAuthCode] = useState('');
const inputRef = useRef<MagicCodeInputHandle | null>(null);
const shouldClearData = account?.needsTwoFactorAuthSetup ?? false;

/**
* Handle text input and clear formError upon text change
Expand Down Expand Up @@ -53,8 +54,8 @@ function BaseTwoFactorAuthForm({account, autoComplete}: BaseTwoFactorAuthFormPro
}

setFormError({});
Session.validateTwoFactorAuth(twoFactorAuthCode);
}, [twoFactorAuthCode, translate]);
Session.validateTwoFactorAuth(twoFactorAuthCode, shouldClearData);
}, [twoFactorAuthCode, shouldClearData, translate]);

useImperativeHandle(ref, () => ({
validateAndSubmitForm() {
Expand Down

0 comments on commit 1084706

Please sign in to comment.