diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index b687abd61bb..bbfcaaacd53 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -50,8 +50,8 @@ import type {HybridAppRoute, Route} from '@src/ROUTES'; import ROUTES from '@src/ROUTES'; import SCREENS from '@src/SCREENS'; import type Credentials from '@src/types/onyx/Credentials'; -import type {AutoAuthState} from '@src/types/onyx/Session'; import type Session from '@src/types/onyx/Session'; +import type {AutoAuthState} from '@src/types/onyx/Session'; import clearCache from './clearCache'; import updateSessionAuthTokens from './updateSessionAuthTokens'; @@ -418,6 +418,21 @@ function beginSignIn(email: string) { API.read(READ_COMMANDS.BEGIN_SIGNIN, params, {optimisticData, successData, failureData}); } +/** + * Create Onyx update to clean up anonymous user data + */ +function buildOnyxDataToCleanUpAnonymousUser() { + const data: Record = {}; + if (session.authTokenType === CONST.AUTH_TOKEN_TYPES.ANONYMOUS && session.accountID) { + data[session.accountID] = null; + } + return { + key: ONYXKEYS.PERSONAL_DETAILS_LIST, + value: data, + onyxMethod: Onyx.METHOD.MERGE, + }; +} + /** * Creates an account for the new user and signs them into the application with the newly created account. * @@ -434,6 +449,8 @@ function signUpUser() { }, ]; + const onyxOperationToCleanUpAnonymousUser = buildOnyxDataToCleanUpAnonymousUser(); + const successData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -442,6 +459,7 @@ function signUpUser() { isLoading: false, }, }, + onyxOperationToCleanUpAnonymousUser, ]; const failureData: OnyxUpdate[] = [ @@ -517,6 +535,8 @@ function signIn(validateCode: string, twoFactorAuthCode?: string) { }, ]; + const onyxOperationToCleanUpAnonymousUser = buildOnyxDataToCleanUpAnonymousUser(); + const successData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -533,6 +553,7 @@ function signIn(validateCode: string, twoFactorAuthCode?: string) { validateCode, }, }, + onyxOperationToCleanUpAnonymousUser, ]; const failureData: OnyxUpdate[] = [ @@ -567,6 +588,7 @@ function signInWithValidateCode(accountID: number, code: string, twoFactorAuthCo // If this is called from the 2fa step, get the validateCode directly from onyx // instead of the one passed from the component state because the state is changing when this method is called. const validateCode = twoFactorAuthCode ? credentials.validateCode : code; + const onyxOperationToCleanUpAnonymousUser = buildOnyxDataToCleanUpAnonymousUser(); const optimisticData: OnyxUpdate[] = [ { @@ -607,6 +629,7 @@ function signInWithValidateCode(accountID: number, code: string, twoFactorAuthCo key: ONYXKEYS.SESSION, value: {autoAuthState: CONST.AUTO_AUTH_STATE.JUST_SIGNED_IN}, }, + onyxOperationToCleanUpAnonymousUser, ]; const failureData: OnyxUpdate[] = [