Skip to content
Merged
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/violet-lobsters-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/elements': patch
---

Fixes a bug where multiple verification codes were sent at once.
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ type Parent = ActorRefFrom<TSignInRouterMachine>;

export type PrepareFirstFactorInput = WithParams<SignInFirstFactor | null> & {
parent: Parent;
resendable: boolean;
};
export type PrepareSecondFactorInput = WithParams<SignInSecondFactor | null> & {
parent: Parent;
resendable: boolean;
};

export type AttemptFirstFactorInput = { parent: Parent; fields: FormFields; currentFactor: SignInFirstFactor | null };
Expand Down Expand Up @@ -171,6 +173,7 @@ const SignInVerificationMachine = setup({
src: 'prepare',
input: ({ context }) => ({
parent: context.parent,
resendable: context.resendable,
params: context.currentFactor as SignInFirstFactor | null,
}),
onDone: {
Expand Down Expand Up @@ -267,10 +270,14 @@ const SignInVerificationMachine = setup({
export const SignInFirstFactorMachine = SignInVerificationMachine.provide({
actors: {
prepare: fromPromise(async ({ input }) => {
const { params, parent } = input;
const { params, parent, resendable } = input;
const clerk = parent.getSnapshot().context.clerk;

if (!params?.strategy || params.strategy === 'password') {
// If a prepare call has already been fired recently, don't re-send
const currentVerificationExpiration = clerk.client.signIn.firstFactorVerification.expireAt;
const needsPrepare = resendable || !currentVerificationExpiration || currentVerificationExpiration < new Date();

if (!params?.strategy || params.strategy === 'password' || !needsPrepare) {
return Promise.resolve(clerk.client.signIn);
}

Expand Down Expand Up @@ -359,12 +366,16 @@ export const SignInFirstFactorMachine = SignInVerificationMachine.provide({
export const SignInSecondFactorMachine = SignInVerificationMachine.provide({
actors: {
prepare: fromPromise(({ input }) => {
const { params, parent } = input;
const { params, parent, resendable } = input;
const clerk = parent.getSnapshot().context.clerk;

// If a prepare call has already been fired recently, don't re-send
const currentVerificationExpiration = clerk.client.signIn.secondFactorVerification.expireAt;
const needsPrepare = resendable || !currentVerificationExpiration || currentVerificationExpiration < new Date();

assertIsDefined(params);

if (params.strategy !== 'phone_code') {
if (params.strategy !== 'phone_code' || !needsPrepare) {
return Promise.resolve(clerk.client.signIn);
}

Expand Down