From 54f0842cbee9356f20ecb15e6909f116f66cdbed Mon Sep 17 00:00:00 2001 From: Andrew Rosiclair Date: Tue, 15 Oct 2024 16:24:25 -0400 Subject: [PATCH 1/7] check for new authTokens --- src/libs/actions/OnyxUpdateManager/index.ts | 33 +++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/OnyxUpdateManager/index.ts b/src/libs/actions/OnyxUpdateManager/index.ts index db00a55aa25b..085e05b0a449 100644 --- a/src/libs/actions/OnyxUpdateManager/index.ts +++ b/src/libs/actions/OnyxUpdateManager/index.ts @@ -1,11 +1,13 @@ -import type {OnyxEntry} from 'react-native-onyx'; +import type {OnyxEntry, OnyxUpdate} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import * as ActiveClientManager from '@libs/ActiveClientManager'; import Log from '@libs/Log'; +import * as NetworkStore from '@libs/Network/NetworkStore'; import * as SequentialQueue from '@libs/Network/SequentialQueue'; import * as App from '@userActions/App'; +import updateSessionAuthTokens from '@userActions/Session/updateSessionAuthTokens'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {OnyxUpdatesFromServer} from '@src/types/onyx'; +import type {OnyxUpdatesFromServer, Session} from '@src/types/onyx'; import {isValidOnyxUpdateFromServer} from '@src/types/onyx/OnyxUpdatesFromServer'; import * as OnyxUpdateManagerUtils from './utils'; import * as DeferredOnyxUpdates from './utils/DeferredOnyxUpdates'; @@ -90,6 +92,10 @@ function handleOnyxUpdateGap(onyxUpdatesFromServer: OnyxEntry): void { + // Consolidate all of the given Onyx updates + const onyxUpdates: OnyxUpdate[] = []; + onyxUpdatesFromServer?.updates?.forEach((updateEvent) => onyxUpdates.push(...updateEvent.data)); + onyxUpdates.push(...(onyxUpdatesFromServer?.response?.onyxData ?? [])); + + // Find any session updates + const sessionUpdates = onyxUpdates?.filter((onyxUpdate) => onyxUpdate.key === ONYXKEYS.SESSION); + + // If any of the updates changes the authToken, let's update it now + sessionUpdates?.forEach((sessionUpdate) => { + const session = (sessionUpdate.value ?? {}) as Session; + const newAuthToken = session.authToken ?? ''; + if (!newAuthToken) { + return; + } + + Log.info('[OnyxUpdateManager] Found an authToken update while handling an Onyx update gap. Updating the authToken.'); + updateSessionAuthTokens(newAuthToken); + NetworkStore.setAuthToken(newAuthToken); + }); +} + export default () => { console.debug('[OnyxUpdateManager] Listening for updates from the server'); Onyx.connect({ From b4093f2c3a6908cc0e37ead4bb215ca680d860ef Mon Sep 17 00:00:00 2001 From: Andrew Rosiclair Date: Tue, 15 Oct 2024 17:31:50 -0400 Subject: [PATCH 2/7] navigate to the enable payments page after validating --- src/components/SettlementButton/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/SettlementButton/index.tsx b/src/components/SettlementButton/index.tsx index f371545ab7b0..ff522a494b1c 100644 --- a/src/components/SettlementButton/index.tsx +++ b/src/components/SettlementButton/index.tsx @@ -190,7 +190,7 @@ function SettlementButton({ if (iouPaymentType === CONST.IOU.PAYMENT_TYPE.EXPENSIFY || iouPaymentType === CONST.IOU.PAYMENT_TYPE.VBBA) { if (!isUserValidated) { - Navigation.navigate(ROUTES.SETTINGS_WALLET_VERIFY_ACCOUNT.getRoute()); + Navigation.navigate(ROUTES.SETTINGS_WALLET_VERIFY_ACCOUNT.getRoute(ROUTES.SETTINGS_ENABLE_PAYMENTS)); return; } triggerKYCFlow(event, iouPaymentType); From 4374ed18dd160a238f88a943c3e4b81625492430 Mon Sep 17 00:00:00 2001 From: Andrew Rosiclair Date: Thu, 17 Oct 2024 16:41:23 -0400 Subject: [PATCH 3/7] use validateLogin --- src/pages/settings/Wallet/VerifyAccountPage.tsx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/pages/settings/Wallet/VerifyAccountPage.tsx b/src/pages/settings/Wallet/VerifyAccountPage.tsx index e375f03ba58c..484ebbb4225e 100644 --- a/src/pages/settings/Wallet/VerifyAccountPage.tsx +++ b/src/pages/settings/Wallet/VerifyAccountPage.tsx @@ -32,6 +32,7 @@ function VerifyAccountPage({route}: VerifyAccountPageProps) { const styles = useThemeStyles(); const validateLoginError = ErrorUtils.getEarliestErrorField(loginData, 'validateLogin'); const [isUserValidated] = useOnyx(ONYXKEYS.USER, {selector: (user) => !!user?.validated}); + const [accountID] = useOnyx(ONYXKEYS.SESSION, {selector: (session) => session?.accountID ?? 0}); const [validateCodeAction] = useOnyx(ONYXKEYS.VALIDATE_ACTION_CODE); @@ -43,10 +44,10 @@ function VerifyAccountPage({route}: VerifyAccountPageProps) { }, []); const handleSubmitForm = useCallback( - (submitCode: string) => { - User.validateSecondaryLogin(loginList, contactMethod ?? '', submitCode); + (validateCode: string) => { + User.validateLogin(accountID ?? 0, validateCode); }, - [loginList, contactMethod], + [accountID], ); const clearError = useCallback(() => { From 81b4a2f7feaa8c5c4464af7b4ae6750b49502e8f Mon Sep 17 00:00:00 2001 From: Andrew Rosiclair Date: Thu, 17 Oct 2024 17:47:29 -0400 Subject: [PATCH 4/7] check both user and account validated state --- src/pages/settings/Wallet/VerifyAccountPage.tsx | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/pages/settings/Wallet/VerifyAccountPage.tsx b/src/pages/settings/Wallet/VerifyAccountPage.tsx index 484ebbb4225e..ff0fa1ba3cb2 100644 --- a/src/pages/settings/Wallet/VerifyAccountPage.tsx +++ b/src/pages/settings/Wallet/VerifyAccountPage.tsx @@ -22,6 +22,7 @@ type VerifyAccountPageProps = StackScreenProps !!user?.validated}); const [accountID] = useOnyx(ONYXKEYS.SESSION, {selector: (session) => session?.accountID ?? 0}); - const [validateCodeAction] = useOnyx(ONYXKEYS.VALIDATE_ACTION_CODE); + // We store validated state in two places so this is a bit of a workaround to check both + const isUserValidated = user?.validated ?? false; + const isAccountValidated = account?.validated ?? false; + const isValidated = isUserValidated || isAccountValidated; + const navigateBackTo = route?.params?.backTo ?? ROUTES.SETTINGS_WALLET; useEffect(() => { @@ -55,11 +59,11 @@ function VerifyAccountPage({route}: VerifyAccountPageProps) { }, [contactMethod]); useEffect(() => { - if (!isUserValidated) { + if (!isValidated) { return; } Navigation.navigate(navigateBackTo); - }, [isUserValidated, navigateBackTo]); + }, [isValidated, navigateBackTo]); return ( Date: Fri, 1 Nov 2024 16:52:49 -0400 Subject: [PATCH 5/7] allow queued onyx updates to be flushed --- src/libs/Network/SequentialQueue.ts | 4 ++-- src/libs/actions/QueuedOnyxUpdates.ts | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/libs/Network/SequentialQueue.ts b/src/libs/Network/SequentialQueue.ts index 643ed64ae7f6..3f4da20c16e1 100644 --- a/src/libs/Network/SequentialQueue.ts +++ b/src/libs/Network/SequentialQueue.ts @@ -138,8 +138,8 @@ function flush() { return; } - if (PersistedRequests.getAll().length === 0) { - Log.info('[SequentialQueue] Unable to flush. No requests to process.'); + if (PersistedRequests.getAll().length === 0 && QueuedOnyxUpdates.isEmpty()) { + Log.info('[SequentialQueue] Unable to flush. No requests or queued Onyx updates to process.'); return; } diff --git a/src/libs/actions/QueuedOnyxUpdates.ts b/src/libs/actions/QueuedOnyxUpdates.ts index 83bc6652cb39..bc19ff12aea1 100644 --- a/src/libs/actions/QueuedOnyxUpdates.ts +++ b/src/libs/actions/QueuedOnyxUpdates.ts @@ -19,4 +19,8 @@ function flushQueue(): Promise { }); } -export {queueOnyxUpdates, flushQueue}; +function isEmpty() { + return queuedOnyxUpdates.length === 0; +} + +export {queueOnyxUpdates, flushQueue, isEmpty}; From 8fc556696496446337b6bc82e87109b8e8b2c24a Mon Sep 17 00:00:00 2001 From: Andrew Rosiclair Date: Fri, 1 Nov 2024 17:24:35 -0400 Subject: [PATCH 6/7] Revert "check both user and account validated state" This reverts commit 81b4a2f7feaa8c5c4464af7b4ae6750b49502e8f. --- src/pages/settings/Wallet/VerifyAccountPage.tsx | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/pages/settings/Wallet/VerifyAccountPage.tsx b/src/pages/settings/Wallet/VerifyAccountPage.tsx index 30b7686ada00..3bd3c2aa7000 100644 --- a/src/pages/settings/Wallet/VerifyAccountPage.tsx +++ b/src/pages/settings/Wallet/VerifyAccountPage.tsx @@ -15,19 +15,14 @@ type VerifyAccountPageProps = StackScreenProps !!user?.validated}); const [accountID] = useOnyx(ONYXKEYS.SESSION, {selector: (session) => session?.accountID ?? 0}); - // We store validated state in two places so this is a bit of a workaround to check both - const isUserValidated = user?.validated ?? false; - const isAccountValidated = account?.validated ?? false; - const isValidated = isUserValidated || isAccountValidated; - const [isValidateCodeActionModalVisible, setIsValidateCodeActionModalVisible] = useState(true); const navigateBackTo = route?.params?.backTo; @@ -46,7 +41,7 @@ function VerifyAccountPage({route}: VerifyAccountPageProps) { }, [contactMethod]); useEffect(() => { - if (!isValidated) { + if (!isUserValidated) { return; } @@ -57,19 +52,19 @@ function VerifyAccountPage({route}: VerifyAccountPageProps) { } Navigation.navigate(navigateBackTo); - }, [isValidated, navigateBackTo]); + }, [isUserValidated, navigateBackTo]); useEffect(() => { if (isValidateCodeActionModalVisible) { return; } - if (!isValidated && navigateBackTo) { + if (!isUserValidated && navigateBackTo) { Navigation.navigate(ROUTES.SETTINGS_WALLET); } else if (!navigateBackTo) { Navigation.goBack(); } - }, [isValidateCodeActionModalVisible, isValidated, navigateBackTo]); + }, [isValidateCodeActionModalVisible, isUserValidated, navigateBackTo]); return ( Date: Thu, 7 Nov 2024 11:44:06 -0500 Subject: [PATCH 7/7] set account.isLoading --- src/libs/actions/User.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index eaccbb8497ac..f3b8b1a15c28 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -555,6 +555,16 @@ function validateLogin(accountID: number, validateCode: string) { Onyx.merge(ONYXKEYS.ACCOUNT, {...CONST.DEFAULT_ACCOUNT_DATA, isLoading: true}); const optimisticData: OnyxUpdate[] = [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.ACCOUNT, + value: { + isLoading: true, + }, + }, + ]; + + const finallyData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.ACCOUNT, @@ -566,7 +576,7 @@ function validateLogin(accountID: number, validateCode: string) { const parameters: ValidateLoginParams = {accountID, validateCode}; - API.write(WRITE_COMMANDS.VALIDATE_LOGIN, parameters, {optimisticData}); + API.write(WRITE_COMMANDS.VALIDATE_LOGIN, parameters, {optimisticData, finallyData}); Navigation.navigate(ROUTES.HOME); }