Skip to content

Commit 51af43d

Browse files
authored
fix(clerk-js): Avoid triggering email code verification twice on React strict mode (#5095)
1 parent 3c75e2c commit 51af43d

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

.changeset/witty-cougars-tickle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
---
4+
5+
Avoid triggering email code verification twice on React strict mode

packages/clerk-js/src/ui/components/SignIn/SignInFactorOneCodeForm.tsx

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { isUserLockedError } from '@clerk/shared/error';
22
import { useClerk } from '@clerk/shared/react';
33
import type { EmailCodeFactor, PhoneCodeFactor, ResetPasswordCodeFactor } from '@clerk/types';
4-
import React from 'react';
54

65
import { clerkInvalidFAPIResponse } from '../../../core/errors';
76
import { useCoreSignIn, useSignInContext } from '../../contexts';
87
import type { VerificationCodeCardProps } from '../../elements';
98
import { useCardState, VerificationCodeCard } from '../../elements';
9+
import { useFetch } from '../../hooks';
1010
import { useSupportEmail } from '../../hooks/useSupportEmail';
1111
import type { LocalizationKey } from '../../localization';
1212
import { useRouter } from '../../router';
@@ -37,23 +37,40 @@ export const SignInFactorOneCodeForm = (props: SignInFactorOneCodeFormProps) =>
3737
const supportEmail = useSupportEmail();
3838
const clerk = useClerk();
3939

40+
const shouldAvoidPrepare = signIn.firstFactorVerification.status === 'verified' && props.factorAlreadyPrepared;
41+
4042
const goBack = () => {
4143
return navigate('../');
4244
};
4345

44-
React.useEffect(() => {
45-
if (!props.factorAlreadyPrepared) {
46-
prepare();
46+
const prepare = () => {
47+
if (shouldAvoidPrepare) {
48+
return;
4749
}
48-
}, []);
4950

50-
const prepare = () => {
5151
void signIn
5252
.prepareFirstFactor(props.factor)
5353
.then(() => props.onFactorPrepare())
5454
.catch(err => handleError(err, [], card.setError));
5555
};
5656

57+
useFetch(
58+
shouldAvoidPrepare
59+
? undefined
60+
: () =>
61+
signIn
62+
?.prepareFirstFactor(props.factor)
63+
.then(() => props.onFactorPrepare())
64+
.catch(err => handleError(err, [], card.setError)),
65+
{
66+
name: 'signIn.prepareFirstFactor',
67+
factor: props.factor,
68+
},
69+
{
70+
staleTime: 100,
71+
},
72+
);
73+
5774
const action: VerificationCodeCardProps['onCodeEntryFinishedAction'] = (code, resolve, reject) => {
5875
signIn
5976
.attemptFirstFactor({ strategy: props.factor.strategy, code })

0 commit comments

Comments
 (0)