1+ import { useClerk } from '@clerk/shared/react' ;
12import type { SignInFactor } from '@clerk/shared/types' ;
23import React from 'react' ;
34
@@ -6,6 +7,7 @@ import { LoadingCard } from '@/ui/elements/LoadingCard';
67
78import { withRedirectToAfterSignIn , withRedirectToSignInTask } from '../../common' ;
89import { useCoreSignIn } from '../../contexts' ;
10+ import { useRouter } from '../../router' ;
911import { SignInFactorTwoAlternativeMethods } from './SignInFactorTwoAlternativeMethods' ;
1012import { SignInFactorTwoBackupCodeCard } from './SignInFactorTwoBackupCodeCard' ;
1113import { SignInFactorTwoEmailCodeCard } from './SignInFactorTwoEmailCodeCard' ;
@@ -26,7 +28,9 @@ const factorKey = (factor: SignInFactor | null | undefined) => {
2628} ;
2729
2830function SignInFactorTwoInternal ( ) : JSX . Element {
31+ const { __internal_setActiveInProgress } = useClerk ( ) ;
2932 const signIn = useCoreSignIn ( ) ;
33+ const router = useRouter ( ) ;
3034 const availableFactors = signIn . supportedSecondFactors ;
3135
3236 const lastPreparedFactorKeyRef = React . useRef ( '' ) ;
@@ -45,6 +49,19 @@ function SignInFactorTwoInternal(): JSX.Element {
4549 toggleAllStrategies ( ) ;
4650 } ;
4751
52+ React . useEffect ( ( ) => {
53+ if ( __internal_setActiveInProgress ) {
54+ return ;
55+ }
56+
57+ // If the sign-in was reset or doesn't exist, redirect back to the start.
58+ // Don't redirect for 'complete' status - setActive will handle navigation.
59+ if ( signIn . status === null || signIn . status === 'needs_identifier' || signIn . status === 'needs_first_factor' ) {
60+ void router . navigate ( '../' ) ;
61+ }
62+ // eslint-disable-next-line react-hooks/exhaustive-deps -- Match SignInFactorOne pattern: only run on mount and when setActiveInProgress changes
63+ } , [ __internal_setActiveInProgress ] ) ;
64+
4865 if ( ! currentFactor ) {
4966 return < LoadingCard /> ;
5067 }
0 commit comments