@@ -5,7 +5,7 @@ import { withCardStateProvider } from '@/ui/elements/contexts';
55import { LoadingCard } from '@/ui/elements/LoadingCard' ;
66
77import { withRedirectToAfterSignIn , withRedirectToSignInTask } from '../../common' ;
8- import { useCoreSignIn } from '../../contexts' ;
8+ import { useCoreSignIn , useSignInContext } from '../../contexts' ;
99import { useRouter } from '../../router' ;
1010import { SignInFactorTwoAlternativeMethods } from './SignInFactorTwoAlternativeMethods' ;
1111import { SignInFactorTwoBackupCodeCard } from './SignInFactorTwoBackupCodeCard' ;
@@ -16,9 +16,10 @@ import { SignInFactorTwoTOTPCard } from './SignInFactorTwoTOTPCard';
1616import { useSecondFactorSelection } from './useSecondFactorSelection' ;
1717
1818function SignInFactorTwoInternal ( ) : JSX . Element {
19- const { __internal_setActiveInProgress } = useClerk ( ) ;
19+ const clerk = useClerk ( ) ;
2020 const signIn = useCoreSignIn ( ) ;
2121 const router = useRouter ( ) ;
22+ const { afterSignInUrl } = useSignInContext ( ) ;
2223 const {
2324 currentFactor,
2425 factorAlreadyPrepared,
@@ -29,17 +30,24 @@ function SignInFactorTwoInternal(): JSX.Element {
2930 } = useSecondFactorSelection ( signIn . supportedSecondFactors ) ;
3031
3132 React . useEffect ( ( ) => {
32- if ( __internal_setActiveInProgress ) {
33+ if ( clerk . __internal_setActiveInProgress ) {
3334 return ;
3435 }
3536
36- // If the sign-in was reset or doesn't exist, redirect back to the start .
37+ // If the sign-in doesn't need second factor verification, redirect away .
3738 // Don't redirect for 'complete' status - setActive will handle navigation.
3839 if ( signIn . status === null || signIn . status === 'needs_identifier' || signIn . status === 'needs_first_factor' ) {
39- void router . navigate ( '../' ) ;
40+ // If the user is already signed in (e.g. multi-session app, page reload after
41+ // successful verification), redirect forward to afterSignInUrl instead of
42+ // back to sign-in start.
43+ if ( clerk . isSignedIn ) {
44+ void router . navigate ( afterSignInUrl ) ;
45+ } else {
46+ void router . navigate ( '../' ) ;
47+ }
4048 }
41- // eslint-disable-next-line react-hooks/exhaustive-deps -- Match SignInFactorOne pattern: only run on mount and when setActiveInProgress changes
42- } , [ __internal_setActiveInProgress ] ) ;
49+ // eslint-disable-next-line react-hooks/exhaustive-deps -- Only run on mount and when setActiveInProgress changes
50+ } , [ clerk . __internal_setActiveInProgress ] ) ;
4351
4452 if ( ! currentFactor ) {
4553 return < LoadingCard /> ;
0 commit comments