Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-signin-factor-two-spinner.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/ui': patch
---

Fix infinite loading spinner when navigating to factor-two sign-in route without an active 2FA session
18 changes: 18 additions & 0 deletions packages/ui/src/components/SignIn/SignInFactorTwo.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { useClerk } from '@clerk/shared/react';
import React from 'react';

import { withCardStateProvider } from '@/ui/elements/contexts';
import { LoadingCard } from '@/ui/elements/LoadingCard';

import { withRedirectToAfterSignIn, withRedirectToSignInTask } from '../../common';
import { useCoreSignIn } from '../../contexts';
import { useRouter } from '../../router';
import { SignInFactorTwoAlternativeMethods } from './SignInFactorTwoAlternativeMethods';
import { SignInFactorTwoBackupCodeCard } from './SignInFactorTwoBackupCodeCard';
import { SignInFactorTwoEmailCodeCard } from './SignInFactorTwoEmailCodeCard';
Expand All @@ -12,7 +16,9 @@ import { SignInFactorTwoTOTPCard } from './SignInFactorTwoTOTPCard';
import { useSecondFactorSelection } from './useSecondFactorSelection';

function SignInFactorTwoInternal(): JSX.Element {
const { __internal_setActiveInProgress } = useClerk();
const signIn = useCoreSignIn();
const router = useRouter();
const {
currentFactor,
factorAlreadyPrepared,
Expand All @@ -22,6 +28,18 @@ function SignInFactorTwoInternal(): JSX.Element {
toggleAllStrategies,
} = useSecondFactorSelection(signIn.supportedSecondFactors);

React.useEffect(() => {
if (__internal_setActiveInProgress) {
return;
}

// If the sign-in doesn't require second factor verification,
// redirect back to the start of the sign-in flow
if (signIn.status !== 'needs_second_factor') {
void router.navigate('../');
}
Comment on lines +38 to +40
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we detect if the user is signed in and redirect to any present redirect url in that case? 🤔

}, [__internal_setActiveInProgress, signIn.status, router]);

if (!currentFactor) {
return <LoadingCard />;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@ describe('SignInFactorTwo', () => {
});

describe('Navigation', () => {
//This isn't yet implemented in the component
it.todo('navigates to SignInStart component if user lands on SignInFactorTwo page but they should not');
it('navigates to SignInStart component if sign-in status is not needs_second_factor', async () => {
const { wrapper, fixtures } = await createFixtures(f => {
f.withEmailAddress();
// Note: We do NOT call f.startSignInFactorTwo() here, so the sign-in status
// will be null/needs_identifier, not 'needs_second_factor'
});
render(<SignInFactorTwo />, { wrapper });
await waitFor(() => {
expect(fixtures.router.navigate).toHaveBeenCalledWith('../');
});
});
});

describe('Submitting', () => {
Expand Down
Loading