mirror of
https://github.com/better-auth/better-auth.git
synced 2026-08-24 14:34:26 -05:00
fix(email-otp): prevent duplicate verification emails when override is enabled
This commit is contained in:
@@ -1012,4 +1012,46 @@ describe("override default email verification", async () => {
|
||||
);
|
||||
expect(sendVerificationOTP).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should send email only once when override is enabled", async () => {
|
||||
let callCountForTestEmail = 0;
|
||||
const sendVerificationOTPFn = vi.fn(async (data, request) => {
|
||||
if (data.email === "test-no-duplicate@email.com") {
|
||||
callCountForTestEmail++;
|
||||
}
|
||||
});
|
||||
|
||||
const { client } = await getTestInstance({
|
||||
emailAndPassword: {
|
||||
enabled: true,
|
||||
},
|
||||
emailVerification: {
|
||||
sendOnSignUp: true,
|
||||
},
|
||||
plugins: [
|
||||
emailOTP({
|
||||
sendVerificationOTP: sendVerificationOTPFn,
|
||||
overrideDefaultEmailVerification: true,
|
||||
sendVerificationOnSignUp: true, // This should be ignored when override is true
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
sendVerificationOTPFn.mockClear();
|
||||
|
||||
await client.signUp.email({
|
||||
email: "test-no-duplicate@email.com",
|
||||
password: "password",
|
||||
name: "Test User",
|
||||
});
|
||||
|
||||
expect(sendVerificationOTPFn).toHaveBeenCalledTimes(1);
|
||||
expect(sendVerificationOTPFn).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
email: "test-no-duplicate@email.com",
|
||||
type: "email-verification",
|
||||
}),
|
||||
expect.any(Object),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1152,7 +1152,8 @@ export const emailOTP = (options: EmailOTPOptions) => {
|
||||
matcher(context) {
|
||||
return !!(
|
||||
context.path?.startsWith("/sign-up") &&
|
||||
opts.sendVerificationOnSignUp
|
||||
opts.sendVerificationOnSignUp &&
|
||||
!opts.overrideDefaultEmailVerification
|
||||
);
|
||||
},
|
||||
handler: createAuthMiddleware(async (ctx) => {
|
||||
|
||||
Reference in New Issue
Block a user