From d0e97b1e709182f59ccc12619836e4cfa7e2cea0 Mon Sep 17 00:00:00 2001 From: dominictb Date: Wed, 28 Aug 2024 13:37:41 +0700 Subject: [PATCH 1/4] fix: clear anonymous user records after login --- src/CONST.ts | 1 + src/libs/actions/Session/index.ts | 36 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/CONST.ts b/src/CONST.ts index 30b0b5364d83..75c688d604c6 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -1339,6 +1339,7 @@ const CONST = { STUDENT_AMBASSADOR: 'studentambassadors@expensify.com', SVFG: 'svfg@expensify.com', EXPENSIFY_EMAIL_DOMAIN: '@expensify.com', + ANONYMOUS_USER_DOMAIN: '@expensify.anon', }, CONCIERGE_DISPLAY_NAME: 'Concierge', diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index b687abd61bb9..4a0e9feca469 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -50,6 +50,7 @@ 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 {PersonalDetailsList} from '@src/types/onyx/PersonalDetails'; import type {AutoAuthState} from '@src/types/onyx/Session'; import type Session from '@src/types/onyx/Session'; import clearCache from './clearCache'; @@ -97,6 +98,15 @@ Onyx.connect({ callback: (val) => (preferredLocale = val ?? null), }); +let allPersonalDetails: OnyxEntry; + +Onyx.connect({ + key: ONYXKEYS.PERSONAL_DETAILS_LIST, + callback: (value) => { + allPersonalDetails = value ?? {}; + }, +}); + function isSupportAuthToken(): boolean { return session.authTokenType === CONST.AUTH_TOKEN_TYPES.SUPPORT; } @@ -418,6 +428,24 @@ 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 anonAccountIDs = Object.values(allPersonalDetails ?? {}) + .filter((detail) => detail?.login?.includes(CONST.EMAIL.ANONYMOUS_USER_DOMAIN) && detail?.accountID) + .map((detail) => detail?.accountID) as number[]; + const data = anonAccountIDs.reduce((res, accountID) => { + res[accountID] = null; + return res; + }, {} as Record); + 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 +462,8 @@ function signUpUser() { }, ]; + const onyxOperationToCleanUpAnonymousUser = buildOnyxDataToCleanUpAnonymousUser(); + const successData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -442,6 +472,7 @@ function signUpUser() { isLoading: false, }, }, + onyxOperationToCleanUpAnonymousUser, ]; const failureData: OnyxUpdate[] = [ @@ -517,6 +548,8 @@ function signIn(validateCode: string, twoFactorAuthCode?: string) { }, ]; + const onyxOperationToCleanUpAnonymousUser = buildOnyxDataToCleanUpAnonymousUser(); + const successData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -533,6 +566,7 @@ function signIn(validateCode: string, twoFactorAuthCode?: string) { validateCode, }, }, + onyxOperationToCleanUpAnonymousUser, ]; const failureData: OnyxUpdate[] = [ @@ -567,6 +601,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 +642,7 @@ function signInWithValidateCode(accountID: number, code: string, twoFactorAuthCo key: ONYXKEYS.SESSION, value: {autoAuthState: CONST.AUTO_AUTH_STATE.JUST_SIGNED_IN}, }, + onyxOperationToCleanUpAnonymousUser, ]; const failureData: OnyxUpdate[] = [ From 24f4854e298b2a62542abd28b69609a6d1de1f7e Mon Sep 17 00:00:00 2001 From: dominictb Date: Fri, 30 Aug 2024 17:57:21 +0700 Subject: [PATCH 2/4] fix: only clear the current anonymous account --- src/libs/actions/Session/index.ts | 71 +++++++++++++------------------ 1 file changed, 29 insertions(+), 42 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 4a0e9feca469..9eb982c32691 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -50,9 +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 {PersonalDetailsList} from '@src/types/onyx/PersonalDetails'; -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'; @@ -98,15 +97,6 @@ Onyx.connect({ callback: (val) => (preferredLocale = val ?? null), }); -let allPersonalDetails: OnyxEntry; - -Onyx.connect({ - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - callback: (value) => { - allPersonalDetails = value ?? {}; - }, -}); - function isSupportAuthToken(): boolean { return session.authTokenType === CONST.AUTH_TOKEN_TYPES.SUPPORT; } @@ -432,13 +422,10 @@ function beginSignIn(email: string) { * Create Onyx update to clean up anonymous user data */ function buildOnyxDataToCleanUpAnonymousUser() { - const anonAccountIDs = Object.values(allPersonalDetails ?? {}) - .filter((detail) => detail?.login?.includes(CONST.EMAIL.ANONYMOUS_USER_DOMAIN) && detail?.accountID) - .map((detail) => detail?.accountID) as number[]; - const data = anonAccountIDs.reduce((res, accountID) => { - res[accountID] = null; - return res; - }, {} as Record); + 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, @@ -1082,39 +1069,39 @@ const canAnonymousUserAccessRoute = (route: string) => { }; export { - beginSignIn, + authenticatePusher, beginAppleSignIn, beginGoogleSignIn, - setSupportAuthToken, + beginSignIn, + canAnonymousUserAccessRoute, checkIfActionIsAllowed, - signIn, - signInWithValidateCode, + cleanupSession, + clearAccountMessages, + clearSignInData, handleExitToNavigation, - signInWithValidateCodeAndNavigate, + hasAuthToken, + hasStashedSession, initAutoAuthState, + invalidateAuthToken, + invalidateCredentials, + isAnonymousUser, + isSupportAuthToken, + reauthenticatePusher, + requestUnlinkValidationLink, + resendValidateCode, + resendValidationLink, + setAccountError, + setSupportAuthToken, + signIn, signInWithShortLivedAuthToken, - cleanupSession, + signInWithSupportAuthToken, + signInWithValidateCode, + signInWithValidateCodeAndNavigate, signOut, signOutAndRedirectToSignIn, - resendValidationLink, - resendValidateCode, - requestUnlinkValidationLink, - unlinkLogin, - clearSignInData, - clearAccountMessages, - setAccountError, - authenticatePusher, - reauthenticatePusher, - invalidateCredentials, - invalidateAuthToken, - isAnonymousUser, + signUpUser, toggleTwoFactorAuth, + unlinkLogin, validateTwoFactorAuth, waitForUserSignIn, - hasAuthToken, - canAnonymousUserAccessRoute, - signInWithSupportAuthToken, - isSupportAuthToken, - hasStashedSession, - signUpUser, }; From 48a7997f914145532d5e8be4ed2550ab6eef1720 Mon Sep 17 00:00:00 2001 From: dominictb Date: Fri, 30 Aug 2024 18:03:15 +0700 Subject: [PATCH 3/4] fix: re-order exports in Session/index.ts --- src/libs/actions/Session/index.ts | 48 +++++++++++++++---------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 9eb982c32691..bbfcaaacd530 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -1069,39 +1069,39 @@ const canAnonymousUserAccessRoute = (route: string) => { }; export { - authenticatePusher, + beginSignIn, beginAppleSignIn, beginGoogleSignIn, - beginSignIn, - canAnonymousUserAccessRoute, - checkIfActionIsAllowed, - cleanupSession, - clearAccountMessages, - clearSignInData, - handleExitToNavigation, - hasAuthToken, - hasStashedSession, - initAutoAuthState, - invalidateAuthToken, - invalidateCredentials, - isAnonymousUser, - isSupportAuthToken, - reauthenticatePusher, - requestUnlinkValidationLink, - resendValidateCode, - resendValidationLink, - setAccountError, setSupportAuthToken, + checkIfActionIsAllowed, signIn, - signInWithShortLivedAuthToken, - signInWithSupportAuthToken, signInWithValidateCode, + handleExitToNavigation, signInWithValidateCodeAndNavigate, + initAutoAuthState, + signInWithShortLivedAuthToken, + cleanupSession, signOut, signOutAndRedirectToSignIn, - signUpUser, - toggleTwoFactorAuth, + resendValidationLink, + resendValidateCode, + requestUnlinkValidationLink, unlinkLogin, + clearSignInData, + clearAccountMessages, + setAccountError, + authenticatePusher, + reauthenticatePusher, + invalidateCredentials, + invalidateAuthToken, + isAnonymousUser, + toggleTwoFactorAuth, validateTwoFactorAuth, waitForUserSignIn, + hasAuthToken, + canAnonymousUserAccessRoute, + signInWithSupportAuthToken, + isSupportAuthToken, + hasStashedSession, + signUpUser, }; From dcfb320e6ccd410b19cf9fd13d6fdf7d344b6233 Mon Sep 17 00:00:00 2001 From: dominictb Date: Thu, 5 Sep 2024 18:57:37 +0700 Subject: [PATCH 4/4] fix: remove redundant constant --- src/CONST.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/CONST.ts b/src/CONST.ts index 6bdc731d775e..603c024ba33e 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -1340,7 +1340,6 @@ const CONST = { STUDENT_AMBASSADOR: 'studentambassadors@expensify.com', SVFG: 'svfg@expensify.com', EXPENSIFY_EMAIL_DOMAIN: '@expensify.com', - ANONYMOUS_USER_DOMAIN: '@expensify.anon', }, CONCIERGE_DISPLAY_NAME: 'Concierge',