Skip to content

Commit bebb14c

Browse files
authored
fix(ui): redirect signed-in users forward from factor-two (#7788)
1 parent ae22220 commit bebb14c

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/ui': patch
3+
---
4+
5+
Redirect signed-in users forward to afterSignInUrl when landing on factor-two without a pending 2FA session, instead of redirecting back to sign-in start

packages/ui/src/components/SignIn/SignInFactorTwo.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { withCardStateProvider } from '@/ui/elements/contexts';
55
import { LoadingCard } from '@/ui/elements/LoadingCard';
66

77
import { withRedirectToAfterSignIn, withRedirectToSignInTask } from '../../common';
8-
import { useCoreSignIn } from '../../contexts';
8+
import { useCoreSignIn, useSignInContext } from '../../contexts';
99
import { useRouter } from '../../router';
1010
import { SignInFactorTwoAlternativeMethods } from './SignInFactorTwoAlternativeMethods';
1111
import { SignInFactorTwoBackupCodeCard } from './SignInFactorTwoBackupCodeCard';
@@ -16,9 +16,10 @@ import { SignInFactorTwoTOTPCard } from './SignInFactorTwoTOTPCard';
1616
import { useSecondFactorSelection } from './useSecondFactorSelection';
1717

1818
function 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

Comments
 (0)