Skip to content

feat(clerk-js): Allow redirect after email link verification #6395

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
23 changes: 18 additions & 5 deletions packages/clerk-js/src/ui/common/EmailLinkVerify.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EmailLinkErrorCodeStatus, isEmailLinkError } from '@clerk/shared/error';
import { useClerk } from '@clerk/shared/react';
import { noop } from '@clerk/shared/utils';
import React from 'react';

import { completeSignUpFlow } from '../../utils';
Expand All @@ -11,16 +12,18 @@ import type { EmailLinkUIStatus } from './EmailLinkStatusCard';
import { EmailLinkStatusCard } from './EmailLinkStatusCard';

export type EmailLinkVerifyProps = {
redirectUrlComplete?: string;
continuePath?: string;
onVerifiedOnOtherDevice?: () => void;
redirectUrl?: string;
redirectUrlComplete?: string;
texts: Record<EmailLinkUIStatus, { title: LocalizationKey; subtitle: LocalizationKey }>;
verifyEmailPath?: string;
verifyPhonePath?: string;
continuePath?: string;
texts: Record<EmailLinkUIStatus, { title: LocalizationKey; subtitle: LocalizationKey }>;
};

export const EmailLinkVerify = (props: EmailLinkVerifyProps) => {
const { redirectUrl, redirectUrlComplete, verifyEmailPath, verifyPhonePath, continuePath } = props;
const { redirectUrl, redirectUrlComplete, verifyEmailPath, verifyPhonePath, continuePath, onVerifiedOnOtherDevice } =
props;
const { handleEmailLinkVerification } = useClerk();
const { navigate } = useRouter();
const signUp = useCoreSignUp();
Expand All @@ -30,7 +33,17 @@ export const EmailLinkVerify = (props: EmailLinkVerifyProps) => {
try {
// Avoid loading flickering
await sleep(750);
await handleEmailLinkVerification({ redirectUrlComplete, redirectUrl }, navigate);
const result = await handleEmailLinkVerification(
{
onVerifiedOnOtherDevice: onVerifiedOnOtherDevice || noop,
redirectUrlComplete,
redirectUrl,
},
navigate,
);

if (result !== null) return;

setVerificationStatus('verified_switch_tab');
await sleep(750);
await completeSignUpFlow({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,40 @@ describe('SignUpEmailLinkFlowComplete', () => {
});

describe('Success', () => {
it('shows the success message when successfully verified', async () => {
it('handles verification and redirects instead of showing success message', async () => {
const { wrapper, fixtures } = await createFixtures(f => {
f.withEmailAddress({ required: true });
});
await runFakeTimers(async timers => {
render(<SignUpEmailLinkFlowComplete />, { wrapper });
timers.runOnlyPendingTimers();
await waitFor(() => expect(fixtures.clerk.handleEmailLinkVerification).toHaveBeenCalled());
screen.getByText(/success/i);
// The component should not show a success message since it should redirect
expect(screen.queryByText(/success/i)).not.toBeInTheDocument();
});
});

it('allows custom onVerifiedOnOtherDevice behavior', async () => {
const customHandler = jest.fn();
const { wrapper, fixtures } = await createFixtures(f => {
f.withEmailAddress({ required: true });
});

// Mock handleEmailLinkVerification to call onVerifiedOnOtherDevice
fixtures.clerk.handleEmailLinkVerification.mockImplementationOnce(async options => {
// Simulate verification happening on a different device
if (options.onVerifiedOnOtherDevice) {
options.onVerifiedOnOtherDevice();
}
return null; // Return null to indicate no redirect happened
});

await runFakeTimers(async timers => {
render(<SignUpEmailLinkFlowComplete onVerifiedOnOtherDevice={customHandler} />, { wrapper });
timers.runOnlyPendingTimers();
await waitFor(() => expect(fixtures.clerk.handleEmailLinkVerification).toHaveBeenCalled());
// The custom handler should be called when verification happens on a different device
expect(customHandler).toHaveBeenCalled();
});
});
});
Expand Down
Loading