diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 371e60959b70..a71310119ab4 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -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, @@ -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); }); } diff --git a/src/pages/settings/Security/TwoFactorAuth/TwoFactorAuthForm/BaseTwoFactorAuthForm.tsx b/src/pages/settings/Security/TwoFactorAuth/TwoFactorAuthForm/BaseTwoFactorAuthForm.tsx index cf226655ce32..ece2e8fdf2f2 100644 --- a/src/pages/settings/Security/TwoFactorAuth/TwoFactorAuthForm/BaseTwoFactorAuthForm.tsx +++ b/src/pages/settings/Security/TwoFactorAuth/TwoFactorAuthForm/BaseTwoFactorAuthForm.tsx @@ -19,6 +19,7 @@ function BaseTwoFactorAuthForm({account, autoComplete}: BaseTwoFactorAuthFormPro const [formError, setFormError] = useState<{twoFactorAuthCode?: string}>({}); const [twoFactorAuthCode, setTwoFactorAuthCode] = useState(''); const inputRef = useRef(null); + const shouldClearData = account?.needsTwoFactorAuthSetup ?? false; /** * Handle text input and clear formError upon text change @@ -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() {