From 4ec56fd12f3a67cd91db0d9930a95f765597ab23 Mon Sep 17 00:00:00 2001 From: seaznCode Date: Sun, 12 Oct 2025 16:09:02 +0200 Subject: [PATCH] feat: Prevent double email sending by adding a ref and reset on failure in EmailVerifyPage --- .../register-email-verify/page.tsx | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/app/quickaction-dashboard/register-email-verify/page.tsx b/src/app/quickaction-dashboard/register-email-verify/page.tsx index e3f671b..51b4892 100644 --- a/src/app/quickaction-dashboard/register-email-verify/page.tsx +++ b/src/app/quickaction-dashboard/register-email-verify/page.tsx @@ -16,12 +16,16 @@ export default function EmailVerifyPage() { const [resendCooldown, setResendCooldown] = useState(0) const [initialEmailSent, setInitialEmailSent] = useState(false) const inputsRef = useRef>([]) + const emailSentRef = useRef(false) // Prevent double email sending // Send verification email automatically on page load useEffect(() => { - if (!token || initialEmailSent) return + if (!token || initialEmailSent || emailSentRef.current) return const sendInitialEmail = async () => { + // Set the ref immediately to prevent race conditions + emailSentRef.current = true + try { const response = await fetch(`${process.env.NEXT_PUBLIC_API_BASE_URL}/api/send-verification-email`, { method: 'POST', @@ -38,9 +42,13 @@ export default function EmailVerifyPage() { setResendCooldown(30) // Start cooldown after initial send } else { console.error('Failed to send initial verification email:', data.message) + // Reset ref on failure so it can be retried + emailSentRef.current = false } } catch (error) { console.error('Error sending initial verification email:', error) + // Reset ref on failure so it can be retried + emailSentRef.current = false } }