From 7ee1ab1d7811f57209f86a79507a7efd5e8e9400 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Wed, 12 Mar 2025 12:09:55 -0700 Subject: [PATCH] fix: remove duplicate verification emails sent on signup --- app/(auth)/login/page.tsx | 33 ++++++++++++--------------------- app/(auth)/signup/page.tsx | 5 ++--- app/(auth)/verify/page.tsx | 36 ++++++++++++++++++++---------------- lib/auth.ts | 2 +- 4 files changed, 35 insertions(+), 41 deletions(-) diff --git a/app/(auth)/login/page.tsx b/app/(auth)/login/page.tsx index de566a6c5b..9a2991c71c 100644 --- a/app/(auth)/login/page.tsx +++ b/app/(auth)/login/page.tsx @@ -42,28 +42,19 @@ export default function LoginPage() { let errorMessage = 'Invalid email or password' if (err.message?.includes('not verified')) { - errorMessage = - 'Please verify your email before signing in. Would you like to resend the verification code?' + // Redirect to verification page directly without asking for confirmation + try { + // Send a new verification OTP + await client.emailOtp.sendVerificationOtp({ + email, + type: 'email-verification', + }) - // Offer to send a verification code and redirect to verification page - const resendVerification = window.confirm( - 'Your email is not verified. Would you like to resend the verification code and go to the verification page?' - ) - - if (resendVerification) { - try { - // Send a new verification OTP - await client.emailOtp.sendVerificationOtp({ - email, - type: 'email-verification', - }) - - // Redirect to the verify page - router.push(`/verify?email=${encodeURIComponent(email)}`) - return - } catch (verifyErr) { - errorMessage = 'Failed to send verification code. Please try again later.' - } + // Redirect to the verify page + router.push(`/verify?email=${encodeURIComponent(email)}`) + return + } catch (verifyErr) { + errorMessage = 'Failed to send verification code. Please try again later.' } } else if (err.message?.includes('not found')) { errorMessage = 'No account found with this email. Please sign up first.' diff --git a/app/(auth)/signup/page.tsx b/app/(auth)/signup/page.tsx index 1c66d7c039..6ccb786199 100644 --- a/app/(auth)/signup/page.tsx +++ b/app/(auth)/signup/page.tsx @@ -36,9 +36,8 @@ export default function SignupPage() { try { await client.signUp.email({ email, password, name }) - // No need to manually send OTP as it's handled by sendVerificationOnSignUp - // Redirect directly to the verify page instead of verify-request - router.push(`/verify?email=${encodeURIComponent(email)}`) + // Pass fromSignup=true to indicate we're coming from signup + router.push(`/verify?email=${encodeURIComponent(email)}&fromSignup=true`) } catch (err: any) { let errorMessage = 'Failed to create account' diff --git a/app/(auth)/verify/page.tsx b/app/(auth)/verify/page.tsx index 7cca0ae033..80a103fbe4 100644 --- a/app/(auth)/verify/page.tsx +++ b/app/(auth)/verify/page.tsx @@ -41,23 +41,27 @@ function VerifyContent() { useEffect(() => { if (email && !isSendingInitialOtp) { setIsSendingInitialOtp(true) - // Send verification OTP on initial page load - client.emailOtp - .sendVerificationOtp({ - email, - type: 'email-verification', - }) - .then(() => {}) - .catch((error) => { - logger.error('Failed to send initial verification code:', error) - addNotification?.( - 'error', - 'Failed to send verification code. Please use the resend button.', - null - ) - }) + + // Only send verification OTP if we're coming from login page + // Skip this if coming from signup since the OTP is already sent + if (!searchParams.get('fromSignup')) { + client.emailOtp + .sendVerificationOtp({ + email, + type: 'email-verification', + }) + .then(() => {}) + .catch((error) => { + logger.error('Failed to send initial verification code:', error) + addNotification?.( + 'error', + 'Failed to send verification code. Please use the resend button.', + null + ) + }) + } } - }, [email, isSendingInitialOtp, addNotification]) + }, [email, isSendingInitialOtp, addNotification, searchParams]) // Enable the verify button when all 6 digits are entered const isOtpComplete = otp.length === 6 diff --git a/lib/auth.ts b/lib/auth.ts index 028e44a52f..8090d19f5e 100644 --- a/lib/auth.ts +++ b/lib/auth.ts @@ -44,7 +44,7 @@ export const auth = betterAuth({ }, emailAndPassword: { enabled: true, - requireEmailVerification: true, + requireEmailVerification: false, sendVerificationOnSignUp: false, throwOnMissingCredentials: true, throwOnInvalidCredentials: true,