Repository navigation
auto-sign in with sign in link in separate browser #90488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9b9d213
ca6e2cf
ccdd6e1
859c304
af0ac5e
6780966
3a8fe41
8f9fc6b
1e19798
5815025
d4aaa3a
aff439d
4700add
a46f9a8
ec856e8
337510f
f2e0f54
1167de4
9ef345a
b489db4
e18daff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import type {NavigationState, PartialState} from '@react-navigation/native'; | ||
| import NAVIGATORS from '@src/NAVIGATORS'; | ||
| import SCREENS from '@src/SCREENS'; | ||
|
|
||
| /** | ||
| * Pure decision logic for the NavigationRoot post-logout reset (kept out of the effect so it | ||
| * stays unit-testable). Returns the state to pass to `navigationRef.reset`, or | ||
| * `undefined` when there is nothing to reset. | ||
| */ | ||
| function getStateToResetAfterLogout(rootState: NavigationState | undefined): NavigationState | PartialState<NavigationState> | undefined { | ||
| const lastRoute = rootState?.routes.at(-1); | ||
| if (!rootState || !lastRoute) { | ||
| return undefined; | ||
| } | ||
|
|
||
| // ValidateLogin's /v/ code is spent by logout; keeping it strands the user. Reset to | ||
| // SCREENS.HOME — this runs only post-logout (NavigationRoot gates on !authenticated), when | ||
| // PublicScreens is mounted and HOME (SignInPage) is its top-level route. | ||
| if (lastRoute.name === SCREENS.VALIDATE_LOGIN) { | ||
| return {index: 0, routes: [{name: SCREENS.HOME}]}; | ||
| } | ||
|
|
||
| // ReportsSplit is shared between logged-in & logged-out; its params can carry stale auth. | ||
| const shouldClearParams = lastRoute.name === NAVIGATORS.REPORTS_SPLIT_NAVIGATOR; | ||
| return { | ||
| ...rootState, | ||
| index: 0, | ||
| routes: [{...lastRoute, params: shouldClearParams ? undefined : lastRoute.params}], | ||
| }; | ||
| } | ||
|
|
||
| export default getStateToResetAfterLogout; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,26 @@ | ||
| import React, {useEffect, useState} from 'react'; | ||
| import {useFocusEffect} from '@react-navigation/native'; | ||
| import React, {useCallback, useEffect, useState} from 'react'; | ||
| import type {OnyxEntry} from 'react-native-onyx'; | ||
| import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator'; | ||
| import ExpiredValidateCodeModal from '@components/ValidateCode/ExpiredValidateCodeModal'; | ||
| import JustSignedInModal from '@components/ValidateCode/JustSignedInModal'; | ||
| import ValidateCodeModal from '@components/ValidateCode/ValidateCodeModal'; | ||
| import useOnyx from '@hooks/useOnyx'; | ||
| import Log from '@libs/Log'; | ||
| import Navigation from '@libs/Navigation/Navigation'; | ||
| import {isValidValidateCode} from '@libs/ValidationUtils'; | ||
| import {handleExitToNavigation, initAutoAuthState, signInWithValidateCode} from '@userActions/Session'; | ||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import ROUTES from '@src/ROUTES'; | ||
| import type {Session as SessionType} from '@src/types/onyx'; | ||
| import type ValidateLoginPageProps from './types'; | ||
|
|
||
| const autoAuthStateSelector = (session: OnyxEntry<SessionType>) => session?.autoAuthState; | ||
|
|
||
| /** If a separate-session magic-link sign-in hasn't completed in this long, it's likely stuck. */ | ||
| const STUCK_DIRECT_SIGN_IN_TIMEOUT_MS = 30 * 1000; | ||
|
|
||
| function ValidateLoginPage({ | ||
| route: { | ||
| params: {accountID, validateCode, exitTo}, | ||
|
|
@@ -38,16 +44,22 @@ function ValidateLoginPage({ | |
| const isUserClickedSignIn = !login && isSignedIn && (autoAuthStateWithDefault === CONST.AUTO_AUTH_STATE.SIGNING_IN || autoAuthStateWithDefault === CONST.AUTO_AUTH_STATE.JUST_SIGNED_IN); | ||
| const shouldStartSignInWithValidateCode = !isUserClickedSignIn && !isSignedIn && (!!login || !!exitTo) && isValidValidateCode(validateCode); | ||
| const isNavigatingToExitTo = isSignedIn && !!exitTo; | ||
| // Fresh-session magic-link sign-in. Not gated on `isSignedIn` because `autoAuthState` lands | ||
| // before `authToken` (separate Onyx broadcasts); that gap would otherwise flash a blank page. | ||
| // Keeps the loader up across SIGNING_IN → JUST_SIGNED_IN until the redirect unmounts the page. | ||
| // Excludes 2FA: it can't complete from here, so the 2FA modal (below) handles it instead of an | ||
| // indefinite loader. | ||
| const isCompletingDirectSignIn = | ||
| !exitTo && !login && !is2FARequired && (autoAuthStateWithDefault === CONST.AUTO_AUTH_STATE.SIGNING_IN || autoAuthStateWithDefault === CONST.AUTO_AUTH_STATE.JUST_SIGNED_IN); | ||
|
mukhrr marked this conversation as resolved.
Resolved
|
||
| const [preferredLocale] = useOnyx(ONYXKEYS.NVP_PREFERRED_LOCALE); | ||
|
|
||
| useEffect(() => { | ||
| setHasInitialized(true); | ||
|
|
||
| if (isUserClickedSignIn) { | ||
| // The user clicked the option to sign in the current tab | ||
| Navigation.isNavigationReady().then(() => { | ||
| Navigation.goBack(); | ||
|
Comment on lines
-47
to
-49
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @MelvinBot which PR introduced goBack logic?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The explicit The The 🤖 Generated by MelvinBot |
||
| }); | ||
| // Just signed in via the magic link with no cached `login` (separate-session sign-in). | ||
| // The redirect Home lives in the focus effect below (not here) so returning to the | ||
| // consumed `/v/...` via browser Back re-fires it instead of getting stuck on the loader. | ||
| return; | ||
| } | ||
| initAutoAuthState(autoAuthStateWithDefault); | ||
|
|
@@ -65,6 +77,22 @@ function ValidateLoginPage({ | |
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, []); | ||
|
|
||
| // Redirect Home after a separate-session magic-link sign-in. On a focus effect (not mount-only) | ||
| // so that returning to the consumed `/v/...` via browser Back re-fires it — the route can linger | ||
| // in the stack when forceReplace is downgraded to a push, and a mount-only effect wouldn't re-run. | ||
| useFocusEffect( | ||
| useCallback(() => { | ||
| if (!isUserClickedSignIn) { | ||
| return; | ||
| } | ||
| // Wait for protected routes (HOME mounts async); forceReplace so Home doesn't stack on the | ||
| // consumed `/v/...` route. | ||
| Navigation.waitForProtectedRoutes().then(() => { | ||
| Navigation.navigate(ROUTES.HOME, {forceReplace: true}); | ||
| }); | ||
| }, [isUserClickedSignIn]), | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| if (!!login || !cachedAccountID || !is2FARequired) { | ||
| if (exitTo) { | ||
|
|
@@ -79,10 +107,24 @@ function ValidateLoginPage({ | |
| }); | ||
| }, [login, cachedAccountID, is2FARequired, exitTo]); | ||
|
|
||
| // waitForProtectedRoutes()/authToken can hang (lazy AuthScreens chunk fails, token | ||
| // never lands). We can't recover the consumed code here, but surface a stuck sign-in to | ||
| // Sentry/Log instead of leaving an indefinitely silent spinner. Observability only — no | ||
| // navigation/UX change. Cleared on unmount (successful redirect) or when the state changes. | ||
| useEffect(() => { | ||
| if (!isCompletingDirectSignIn) { | ||
| return; | ||
| } | ||
| const timeoutID = setTimeout(() => { | ||
| Log.alert('[ValidateLoginPage] Magic-link sign-in appears stuck (protected routes / authToken not ready)', {autoAuthState: autoAuthStateWithDefault}); | ||
| }, STUCK_DIRECT_SIGN_IN_TIMEOUT_MS); | ||
| return () => clearTimeout(timeoutID); | ||
| }, [isCompletingDirectSignIn, autoAuthStateWithDefault]); | ||
|
|
||
| return ( | ||
| <> | ||
| {autoAuthStateWithDefault === CONST.AUTO_AUTH_STATE.FAILED && !is2FARequired && <ExpiredValidateCodeModal />} | ||
| {(autoAuthStateWithDefault === CONST.AUTO_AUTH_STATE.JUST_SIGNED_IN || autoAuthStateWithDefault === CONST.AUTO_AUTH_STATE.FAILED) && is2FARequired && !isSignedIn && !!login && ( | ||
| {(autoAuthStateWithDefault === CONST.AUTO_AUTH_STATE.JUST_SIGNED_IN || autoAuthStateWithDefault === CONST.AUTO_AUTH_STATE.FAILED) && is2FARequired && !isSignedIn && ( | ||
| <JustSignedInModal is2FARequired /> | ||
| )} | ||
| {autoAuthStateWithDefault === CONST.AUTO_AUTH_STATE.JUST_SIGNED_IN && isSignedIn && !exitTo && !!login && <JustSignedInModal is2FARequired={false} />} | ||
|
|
@@ -93,8 +135,9 @@ function ValidateLoginPage({ | |
| code={validateCode} | ||
| /> | ||
| )} | ||
| {(!effectiveAutoAuthState ? shouldStartSignInWithValidateCode : autoAuthStateWithDefault === CONST.AUTO_AUTH_STATE.SIGNING_IN) && ( | ||
| {((!effectiveAutoAuthState ? shouldStartSignInWithValidateCode : autoAuthStateWithDefault === CONST.AUTO_AUTH_STATE.SIGNING_IN) || isCompletingDirectSignIn) && ( | ||
| <FullScreenLoadingIndicator | ||
| testID="validate-login-loading" | ||
| reasonAttributes={{ | ||
| context: 'ValidateLoginPage', | ||
| isSigningIn: autoAuthStateWithDefault === CONST.AUTO_AUTH_STATE.SIGNING_IN, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the highest-risk change and it affects all users, not the magic-link flow.
AuthScreensislazy(... "authScreens.prefetch"). On a cold start, before the prefetch resolves, this top-level Suspense now renders aLoadingIndicatorinstead ofnull. Previouslynulllet the native splash cover the gap silently. There's a real risk of a spinner flash after the splash hides on native cold start, or a double-loader against the existing app-level loading state. This needs native cold-start testing on a slow device — the PR's screenshots only cover the magic-link flow.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mkhutornyi I agree. This is risky. But without this we have a blank page between two loading spinner pages as mentioned here #90488 (comment)