Skip to content

Commit

Permalink
move sign in logic to HybridApp.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
war-in committed Nov 14, 2024
1 parent 703c31d commit 1f6a0e1
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 116 deletions.
1 change: 0 additions & 1 deletion src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6142,7 +6142,6 @@ const CONST = {
WAITING_FOR_SIGN_OUT: 'waitingForSignOut',
RETRYING_AFTER_FAILURE: 'retryingAfterFailure',
FAILED_AGAIN: 'failedAgain',
BLOCKED: 'blocked',
},

CSV_IMPORT_COLUMNS: {
Expand Down
95 changes: 92 additions & 3 deletions src/libs/HybridApp.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,114 @@
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<TryNewDot>;
Onyx.connect({
key: ONYXKEYS.NVP_TRYNEWDOT,
callback: (newTryNewDot) => {
tryNewDot = newTryNewDot;
},
});

let credentials: OnyxEntry<Credentials>;
Onyx.connect({
key: ONYXKEYS.CREDENTIALS,
callback: (newCredentials) => {
credentials = newCredentials;
},
});

let currentHybridApp: OnyxEntry<HybridApp>;
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');

Check failure on line 50 in src/libs/HybridApp.ts

View workflow job for this annotation

GitHub Actions / ESLint check

Unexpected console statement

Check failure on line 50 in src/libs/HybridApp.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Unexpected console statement
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);
} else {
NativeModules.HybridAppModule.closeReactNativeApp(false, true);
}
}

console.log('[Hybridapp] should use old app', shouldUseOldApp(tryNewDot));

Check failure on line 71 in src/libs/HybridApp.ts

View workflow job for this annotation

GitHub Actions / ESLint check

Unexpected console statement

Check failure on line 71 in src/libs/HybridApp.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Unexpected console statement
if (hybridApp?.oldDotSignInState === CONST.HYBRID_APP_SIGN_IN_STATE.FINISHED && shouldUseOldApp(tryNewDot)) {
if (hybridApp?.oldDotSignInError) {
return;
}
console.log('closing!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');

Check failure on line 76 in src/libs/HybridApp.ts

View workflow job for this annotation

GitHub Actions / ESLint check

Unexpected console statement

Check failure on line 76 in src/libs/HybridApp.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Unexpected console statement
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');

Check failure on line 92 in src/libs/HybridApp.ts

View workflow job for this annotation

GitHub Actions / ESLint check

Unexpected console statement

Check failure on line 92 in src/libs/HybridApp.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Unexpected console statement
setOldDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.STARTED);
NativeModules.HybridAppModule.signInToOldDot(credentials.autoGeneratedLogin, credentials.autoGeneratedPassword);
}

currentHybridApp = hybridApp;
},
});

let currentSession: OnyxEntry<Session>;
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (session: OnyxEntry<Session>) => {
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;
Expand All @@ -43,3 +130,5 @@ const init: Init = () => {
};

export default {init};

export {shouldUseOldApp};
1 change: 1 addition & 0 deletions src/libs/actions/SignInRedirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function clearStorageAndRedirect(errorMessage?: string): Promise<void> {
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.
Expand Down
23 changes: 7 additions & 16 deletions src/pages/settings/InitialSettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Check failure on line 41 in src/pages/settings/InitialSettingsPage.tsx

View workflow job for this annotation

GitHub Actions / ESLint check

'setNewDotSignInState' is defined but never used

Check failure on line 41 in src/pages/settings/InitialSettingsPage.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'setNewDotSignInState' is defined but never used
import * as Link from '@userActions/Link';
import * as PaymentMethods from '@userActions/PaymentMethods';
import * as Session from '@userActions/Session';
Expand Down Expand Up @@ -256,7 +256,6 @@ function InitialSettingsPage({currentUserPersonalDetails}: InitialSettingsPagePr
action: () => {
console.log('[HybridApp] ready to switch', hybridApp);

Check failure on line 257 in src/pages/settings/InitialSettingsPage.tsx

View workflow job for this annotation

GitHub Actions / ESLint check

Unexpected console statement

Check failure on line 257 in src/pages/settings/InitialSettingsPage.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Unexpected console statement
if (hybridApp?.readyToSwitchToClassicExperience) {
setOldDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.BLOCKED);
NativeModules.HybridAppModule.closeReactNativeApp(false, true);
setInitialURL(undefined);
return;
Expand All @@ -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);
}
},
}
Expand Down Expand Up @@ -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
Expand Down
99 changes: 3 additions & 96 deletions src/pages/signin/ValidateCodeForm/BaseValidateCodeForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -58,10 +49,6 @@ type ValidateCodeFormVariant = 'validateCode' | 'twoFactorAuthCode' | 'recoveryC

type FormError = Partial<Record<ValidateCodeFormVariant, TranslationPaths>>;

function shouldUseOldApp(tryNewDot?: TryNewDot) {
return tryNewDot?.classicRedirect.dismissed === true || tryNewDot?.classicRedirect.dismissed === 'true';
}

function BaseValidateCodeForm({autoComplete, isUsingRecoveryCode, setIsUsingRecoveryCode, isVisible}: BaseValidateCodeFormProps, forwardedRef: ForwardedRef<BaseValidateCodeFormRef>) {
const [account] = useOnyx(ONYXKEYS.ACCOUNT);
const [credentials] = useOnyx(ONYXKEYS.CREDENTIALS);
Expand Down Expand Up @@ -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);

Check failure on line 84 in src/pages/signin/ValidateCodeForm/BaseValidateCodeForm.tsx

View workflow job for this annotation

GitHub Actions / ESLint check

Unexpected console statement

Check failure on line 84 in src/pages/signin/ValidateCodeForm/BaseValidateCodeForm.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Unexpected console statement

useEffect(() => {
if (!(inputValidateCodeRef.current && hasError && (session?.autoAuthState === CONST.AUTO_AUTH_STATE.FAILED || account?.isLoading))) {
Expand Down

0 comments on commit 1f6a0e1

Please sign in to comment.