From 6abb89f99f27ddcd6f005ff4eb3c16c377c69880 Mon Sep 17 00:00:00 2001 From: Rushat Gabhane Date: Wed, 24 Jul 2024 05:55:59 +0300 Subject: [PATCH 1/4] reset onyx data for forced 2fa --- src/libs/actions/Session/index.ts | 32 ++++++++++++------- .../BaseTwoFactorAuthForm.tsx | 5 +-- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 371e60959b70..581ffebde1f1 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, shouldResetData: 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 (shouldResetData) { + 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..e27e180145fb 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 shouldResetData = account?.needsTwoFactorAuthSetup; /** * 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, shouldResetData); + }, [twoFactorAuthCode, shouldResetData, translate]); useImperativeHandle(ref, () => ({ validateAndSubmitForm() { From 130e626e07c14d95de59446d18f7e5dcb81e51aa Mon Sep 17 00:00:00 2001 From: Rushat Gabhane Date: Wed, 24 Jul 2024 05:56:26 +0300 Subject: [PATCH 2/4] clean up --- src/libs/actions/Session/index.ts | 4 ++-- .../TwoFactorAuthForm/BaseTwoFactorAuthForm.tsx | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 581ffebde1f1..a71310119ab4 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -923,7 +923,7 @@ function updateAuthTokenAndOpenApp(authToken?: string, encryptedAuthToken?: stri openApp(); } -function validateTwoFactorAuth(twoFactorAuthCode: string, shouldResetData: boolean) { +function validateTwoFactorAuth(twoFactorAuthCode: string, shouldClearData: boolean) { const optimisticData = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -963,7 +963,7 @@ function validateTwoFactorAuth(twoFactorAuthCode: string, shouldResetData: boole } // Clear onyx data if the user has just signed in and is forced to add 2FA - if (shouldResetData) { + if (shouldClearData) { const keysToPreserveWithPrivatePersonalDetails = [...KEYS_TO_PRESERVE, ONYXKEYS.PRIVATE_PERSONAL_DETAILS]; Onyx.clear(keysToPreserveWithPrivatePersonalDetails).then(() => updateAuthTokenAndOpenApp(response.authToken, response.encryptedAuthToken)); return; diff --git a/src/pages/settings/Security/TwoFactorAuth/TwoFactorAuthForm/BaseTwoFactorAuthForm.tsx b/src/pages/settings/Security/TwoFactorAuth/TwoFactorAuthForm/BaseTwoFactorAuthForm.tsx index e27e180145fb..ece2e8fdf2f2 100644 --- a/src/pages/settings/Security/TwoFactorAuth/TwoFactorAuthForm/BaseTwoFactorAuthForm.tsx +++ b/src/pages/settings/Security/TwoFactorAuth/TwoFactorAuthForm/BaseTwoFactorAuthForm.tsx @@ -19,7 +19,7 @@ function BaseTwoFactorAuthForm({account, autoComplete}: BaseTwoFactorAuthFormPro const [formError, setFormError] = useState<{twoFactorAuthCode?: string}>({}); const [twoFactorAuthCode, setTwoFactorAuthCode] = useState(''); const inputRef = useRef(null); - const shouldResetData = account?.needsTwoFactorAuthSetup; + const shouldClearData = account?.needsTwoFactorAuthSetup ?? false; /** * Handle text input and clear formError upon text change @@ -54,8 +54,8 @@ function BaseTwoFactorAuthForm({account, autoComplete}: BaseTwoFactorAuthFormPro } setFormError({}); - Session.validateTwoFactorAuth(twoFactorAuthCode, shouldResetData); - }, [twoFactorAuthCode, shouldResetData, translate]); + Session.validateTwoFactorAuth(twoFactorAuthCode, shouldClearData); + }, [twoFactorAuthCode, shouldClearData, translate]); useImperativeHandle(ref, () => ({ validateAndSubmitForm() { From 0a9c4e6af569666ce4ff61bd0c43647dae90d286 Mon Sep 17 00:00:00 2001 From: Rushat Gabhane Date: Wed, 24 Jul 2024 05:59:34 +0300 Subject: [PATCH 3/4] commie --- src/libs/actions/Session/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index a71310119ab4..55d70a9e815f 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -962,7 +962,7 @@ function validateTwoFactorAuth(twoFactorAuthCode: string, shouldClearData: boole return; } - // Clear onyx data if the user has just signed in and is forced to add 2FA + // When the user is forced to add 2FA, we should clear Onyx data. if (shouldClearData) { const keysToPreserveWithPrivatePersonalDetails = [...KEYS_TO_PRESERVE, ONYXKEYS.PRIVATE_PERSONAL_DETAILS]; Onyx.clear(keysToPreserveWithPrivatePersonalDetails).then(() => updateAuthTokenAndOpenApp(response.authToken, response.encryptedAuthToken)); From 52d68a03b1af7567a0a35292d3edfef8f2ed1947 Mon Sep 17 00:00:00 2001 From: Rushat Gabhane Date: Wed, 24 Jul 2024 06:00:29 +0300 Subject: [PATCH 4/4] comment patch --- src/libs/actions/Session/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 55d70a9e815f..a71310119ab4 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -962,7 +962,7 @@ function validateTwoFactorAuth(twoFactorAuthCode: string, shouldClearData: boole return; } - // When the user is forced to add 2FA, we should clear Onyx data. + // 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));