From 1f6a0e16c5a155a41f306807453efc7d87c31262 Mon Sep 17 00:00:00 2001 From: war-in Date: Thu, 14 Nov 2024 17:38:41 +0100 Subject: [PATCH] move sign in logic to HybridApp.ts --- src/CONST.ts | 1 - src/libs/HybridApp.ts | 95 +++++++++++++++++- src/libs/actions/SignInRedirect.ts | 1 + src/pages/settings/InitialSettingsPage.tsx | 23 ++--- .../ValidateCodeForm/BaseValidateCodeForm.tsx | 99 +------------------ 5 files changed, 103 insertions(+), 116 deletions(-) diff --git a/src/CONST.ts b/src/CONST.ts index 0922f57bc3d6..8ce03a631c3b 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -6142,7 +6142,6 @@ const CONST = { WAITING_FOR_SIGN_OUT: 'waitingForSignOut', RETRYING_AFTER_FAILURE: 'retryingAfterFailure', FAILED_AGAIN: 'failedAgain', - BLOCKED: 'blocked', }, CSV_IMPORT_COLUMNS: { diff --git a/src/libs/HybridApp.ts b/src/libs/HybridApp.ts index 924eac9130aa..4ebc9563df53 100644 --- a/src/libs/HybridApp.ts +++ b/src/libs/HybridApp.ts @@ -1,16 +1,65 @@ import {DeviceEventEmitter, NativeModules} from 'react-native'; -import Onyx, {OnyxEntry} from 'react-native-onyx'; +import Onyx from 'react-native-onyx'; +import type {OnyxEntry} from 'react-native-onyx'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {HybridApp} from '@src/types/onyx'; -import {setIsSigningIn, setOldDotSignInError, setOldDotSignInState, setReadyToSwitchToClassicExperience} from './actions/HybridApp'; +import type {Credentials, HybridApp, Session, TryNewDot} from '@src/types/onyx'; +import { + setIsSigningIn, + setNewDotSignInState, + setOldDotSignInError, + setOldDotSignInState, + setReadyToShowAuthScreens, + setReadyToSwitchToClassicExperience, + setShouldResetSigningInLogic, + setUseNewDotSignInPage, +} from './actions/HybridApp'; import type {Init} from './ActiveClientManager/types'; import Log from './Log'; +function shouldUseOldApp(tryNewDot?: TryNewDot) { + return tryNewDot?.classicRedirect.dismissed === true || tryNewDot?.classicRedirect.dismissed === 'true'; +} + +let tryNewDot: OnyxEntry; +Onyx.connect({ + key: ONYXKEYS.NVP_TRYNEWDOT, + callback: (newTryNewDot) => { + tryNewDot = newTryNewDot; + }, +}); + +let credentials: OnyxEntry; +Onyx.connect({ + key: ONYXKEYS.CREDENTIALS, + callback: (newCredentials) => { + credentials = newCredentials; + }, +}); + let currentHybridApp: OnyxEntry; Onyx.connect({ key: ONYXKEYS.HYBRID_APP, callback: (hybridApp) => { + if (!NativeModules.HybridAppModule) { + return; + } + + // reset sign in logic + if (hybridApp?.shouldResetSigningInLogic) { + console.log('[HybridApp] Resetting sign in flow'); + setReadyToShowAuthScreens(false); + setReadyToSwitchToClassicExperience(false); + setIsSigningIn(false); + setUseNewDotSignInPage(true); + setOldDotSignInError(null); + + setOldDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.NOT_STARTED); + setNewDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.NOT_STARTED); + setShouldResetSigningInLogic(false); + return; + } + if (currentHybridApp?.oldDotSignInState === CONST.HYBRID_APP_SIGN_IN_STATE.RETRYING_AFTER_FAILURE && hybridApp?.oldDotSignInState === CONST.HYBRID_APP_SIGN_IN_STATE.FINISHED) { if (hybridApp?.oldDotSignInError) { setOldDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.FAILED_AGAIN); @@ -18,10 +67,48 @@ Onyx.connect({ NativeModules.HybridAppModule.closeReactNativeApp(false, true); } } + + console.log('[Hybridapp] should use old app', shouldUseOldApp(tryNewDot)); + if (hybridApp?.oldDotSignInState === CONST.HYBRID_APP_SIGN_IN_STATE.FINISHED && shouldUseOldApp(tryNewDot)) { + if (hybridApp?.oldDotSignInError) { + return; + } + console.log('closing!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'); + NativeModules.HybridAppModule.closeReactNativeApp(false, false); + } + + if ( + hybridApp?.newDotSignInState === CONST.HYBRID_APP_SIGN_IN_STATE.FINISHED && + hybridApp?.oldDotSignInState === CONST.HYBRID_APP_SIGN_IN_STATE.NOT_STARTED && + credentials?.autoGeneratedLogin && + credentials?.autoGeneratedPassword + ) { + if (shouldUseOldApp(tryNewDot)) { + setIsSigningIn(true); + } else { + setReadyToShowAuthScreens(true); + } + + console.log('[HybridApp] signInToOldDot'); + setOldDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.STARTED); + NativeModules.HybridAppModule.signInToOldDot(credentials.autoGeneratedLogin, credentials.autoGeneratedPassword); + } + currentHybridApp = hybridApp; }, }); +let currentSession: OnyxEntry; +Onyx.connect({ + key: ONYXKEYS.SESSION, + callback: (session: OnyxEntry) => { + if (!currentSession?.authToken && session?.authToken && currentHybridApp?.newDotSignInState === CONST.HYBRID_APP_SIGN_IN_STATE.STARTED) { + setNewDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.FINISHED); + } + currentSession = session; + }, +}); + const init: Init = () => { if (!NativeModules.HybridAppModule) { return; @@ -43,3 +130,5 @@ const init: Init = () => { }; export default {init}; + +export {shouldUseOldApp}; diff --git a/src/libs/actions/SignInRedirect.ts b/src/libs/actions/SignInRedirect.ts index e612b56aa794..3042ddda6845 100644 --- a/src/libs/actions/SignInRedirect.ts +++ b/src/libs/actions/SignInRedirect.ts @@ -23,6 +23,7 @@ function clearStorageAndRedirect(errorMessage?: string): Promise { keysToPreserve.push(ONYXKEYS.NVP_PREFERRED_LOCALE); keysToPreserve.push(ONYXKEYS.ACTIVE_CLIENTS); keysToPreserve.push(ONYXKEYS.DEVICE_ID); + keysToPreserve.push(ONYXKEYS.HYBRID_APP); // After signing out, set ourselves as offline if we were offline before logging out and we are not forcing it. // If we are forcing offline, ignore it while signed out, otherwise it would require a refresh because there's no way to toggle the switch to go back online while signed out. diff --git a/src/pages/settings/InitialSettingsPage.tsx b/src/pages/settings/InitialSettingsPage.tsx index 5ebe39086f8c..e441c6b9c09f 100755 --- a/src/pages/settings/InitialSettingsPage.tsx +++ b/src/pages/settings/InitialSettingsPage.tsx @@ -38,7 +38,7 @@ import {hasGlobalWorkspaceSettingsRBR} from '@libs/WorkspacesSettingsUtils'; import * as ReportActionContextMenu from '@pages/home/report/ContextMenu/ReportActionContextMenu'; import variables from '@styles/variables'; import * as App from '@userActions/App'; -import {setIsSigningIn, setNewDotSignInState, setOldDotSignInError, setOldDotSignInState} from '@userActions/HybridApp'; +import {setIsSigningIn, setNewDotSignInState, setOldDotSignInError, setOldDotSignInState, setShouldResetSigningInLogic} from '@userActions/HybridApp'; import * as Link from '@userActions/Link'; import * as PaymentMethods from '@userActions/PaymentMethods'; import * as Session from '@userActions/Session'; @@ -256,7 +256,6 @@ function InitialSettingsPage({currentUserPersonalDetails}: InitialSettingsPagePr action: () => { console.log('[HybridApp] ready to switch', hybridApp); if (hybridApp?.readyToSwitchToClassicExperience) { - setOldDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.BLOCKED); NativeModules.HybridAppModule.closeReactNativeApp(false, true); setInitialURL(undefined); return; @@ -269,8 +268,7 @@ function InitialSettingsPage({currentUserPersonalDetails}: InitialSettingsPagePr setOldDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.RETRYING_AFTER_FAILURE); } else { signOutAndRedirectToSignIn(undefined, undefined, false); - setNewDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.NOT_STARTED); - setOldDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.NOT_STARTED); + setShouldResetSigningInLogic(true); } }, } @@ -298,21 +296,14 @@ function InitialSettingsPage({currentUserPersonalDetails}: InitialSettingsPagePr { translationKey: signOutTranslationKey, icon: Expensicons.Exit, - action: () => signOut(false), + action: () => { + signOut(false); + setShouldResetSigningInLogic(true); + }, }, ], }; - }, [ - styles.pt4, - styles.popoverMenuIcon, - hybridApp?.isSigningIn, - hybridApp?.readyToSwitchToClassicExperience, - theme.spinner, - setInitialURL, - credentials?.autoGeneratedLogin, - credentials?.autoGeneratedPassword, - signOut, - ]); + }, [styles.pt4, styles.popoverMenuIcon, hybridApp, theme.spinner, credentials?.autoGeneratedLogin, credentials?.autoGeneratedPassword, setInitialURL, signOut]); /** * Retuns JSX.Element with menu items diff --git a/src/pages/signin/ValidateCodeForm/BaseValidateCodeForm.tsx b/src/pages/signin/ValidateCodeForm/BaseValidateCodeForm.tsx index 9a071ab9e305..51ffb03ec81b 100755 --- a/src/pages/signin/ValidateCodeForm/BaseValidateCodeForm.tsx +++ b/src/pages/signin/ValidateCodeForm/BaseValidateCodeForm.tsx @@ -21,26 +21,17 @@ import useThemeStyles from '@hooks/useThemeStyles'; import AccountUtils from '@libs/AccountUtils'; import canFocusInputOnScreenFocus from '@libs/canFocusInputOnScreenFocus'; import * as ErrorUtils from '@libs/ErrorUtils'; +import {shouldUseOldApp} from '@libs/HybridApp'; import * as ValidationUtils from '@libs/ValidationUtils'; import ChangeExpensifyLoginLink from '@pages/signin/ChangeExpensifyLoginLink'; import Terms from '@pages/signin/Terms'; -import { - setIsSigningIn, - setNewDotSignInState, - setOldDotSignInError, - setOldDotSignInState, - setReadyToShowAuthScreens, - setReadyToSwitchToClassicExperience, - setShouldResetSigningInLogic, - setUseNewDotSignInPage, -} from '@userActions/HybridApp'; +import {setIsSigningIn, setNewDotSignInState, setOldDotSignInError, setOldDotSignInState, setShouldResetSigningInLogic} from '@userActions/HybridApp'; import * as SessionActions from '@userActions/Session'; import {signOut} from '@userActions/Session'; import * as User from '@userActions/User'; import CONST from '@src/CONST'; import type {TranslationPaths} from '@src/languages/types'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {TryNewDot} from '@src/types/onyx'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import type ValidateCodeFormProps from './types'; @@ -58,10 +49,6 @@ type ValidateCodeFormVariant = 'validateCode' | 'twoFactorAuthCode' | 'recoveryC type FormError = Partial>; -function shouldUseOldApp(tryNewDot?: TryNewDot) { - return tryNewDot?.classicRedirect.dismissed === true || tryNewDot?.classicRedirect.dismissed === 'true'; -} - function BaseValidateCodeForm({autoComplete, isUsingRecoveryCode, setIsUsingRecoveryCode, isVisible}: BaseValidateCodeFormProps, forwardedRef: ForwardedRef) { const [account] = useOnyx(ONYXKEYS.ACCOUNT); const [credentials] = useOnyx(ONYXKEYS.CREDENTIALS); @@ -94,87 +81,7 @@ function BaseValidateCodeForm({autoComplete, isUsingRecoveryCode, setIsUsingReco const shouldDisableResendValidateCode = isOffline ?? account?.isLoading; const isValidateCodeFormSubmitting = AccountUtils.isValidateCodeFormSubmitting(account); - useEffect(() => { - if (!NativeModules.HybridAppModule) { - return; - } - if (!hybridApp?.shouldResetSigningInLogic) { - return; - } - console.log('[HybridApp] Resetting sign in flow'); - setReadyToShowAuthScreens(false); - setReadyToSwitchToClassicExperience(false); - setIsSigningIn(false); - setUseNewDotSignInPage(true); - setOldDotSignInError(null); - - setOldDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.NOT_STARTED); - setNewDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.NOT_STARTED); - setShouldResetSigningInLogic(false); - }, [hybridApp?.shouldResetSigningInLogic]); - - /** - * ustawianie stanu logowania ND na finished, jak używamy strony logowania - */ - useEffect(() => { - if (!NativeModules.HybridAppModule || !hybridApp?.useNewDotSignInPage) { - return; - } - - if (hybridApp?.newDotSignInState === CONST.HYBRID_APP_SIGN_IN_STATE.STARTED && !isValidateCodeFormSubmitting && !!session?.authToken) { - console.log('[HybridApp] session auth token', session?.authToken); - setNewDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.FINISHED); - } - }, [isValidateCodeFormSubmitting, session?.authToken, hybridApp?.useNewDotSignInPage, hybridApp?.newDotSignInState]); - - /** - * główny efekt obsługujacy kilka rzeczy - * - odpalanie OD sign in po skończeniu logowania do ND - * - jak chcemy używać ND to w tym momencie jesteśmy gotowi i pokazujemy auth screens - * - jeśli skońćzyło się logowanie do OD i chcemy używać starej apki to zamykamy RN - */ - useEffect(() => { - if (!NativeModules.HybridAppModule || !hybridApp?.useNewDotSignInPage || hybridApp?.oldDotSignInState === CONST.HYBRID_APP_SIGN_IN_STATE.BLOCKED) { - return; - } - - console.log('[HybridApp] newDotSignInState', hybridApp?.newDotSignInState); - console.log('[HybridApp] oldDotSignInState', hybridApp?.oldDotSignInState); - if ( - hybridApp?.newDotSignInState === CONST.HYBRID_APP_SIGN_IN_STATE.FINISHED && - hybridApp?.oldDotSignInState === CONST.HYBRID_APP_SIGN_IN_STATE.NOT_STARTED && - credentials?.autoGeneratedLogin && - credentials?.autoGeneratedPassword - ) { - if (shouldUseOldApp(tryNewDot)) { - setIsSigningIn(true); - } else { - setReadyToShowAuthScreens(true); - } - - console.log('[HybridApp] signInToOldDot'); - setOldDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.STARTED); - NativeModules.HybridAppModule.signInToOldDot(credentials.autoGeneratedLogin, credentials.autoGeneratedPassword); - } - - console.log('[Hybridapp] should use old app', shouldUseOldApp(tryNewDot)); - if (hybridApp?.oldDotSignInState === CONST.HYBRID_APP_SIGN_IN_STATE.FINISHED && shouldUseOldApp(tryNewDot)) { - if (hybridApp?.oldDotSignInError) { - return; - } - console.log('closing!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'); - NativeModules.HybridAppModule.closeReactNativeApp(false, false); - } - }, [ - account?.isLoading, - credentials?.autoGeneratedLogin, - credentials?.autoGeneratedPassword, - tryNewDot, - hybridApp?.useNewDotSignInPage, - hybridApp?.oldDotSignInError, - hybridApp?.newDotSignInState, - hybridApp?.oldDotSignInState, - ]); + console.log('[HybridApp] hybridApp', hybridApp); useEffect(() => { if (!(inputValidateCodeRef.current && hasError && (session?.autoAuthState === CONST.AUTO_AUTH_STATE.FAILED || account?.isLoading))) {