feat: Prevent double email sending by adding a ref and reset on failure in EmailVerifyPage

This commit is contained in:
seaznCode 2025-10-12 16:09:02 +02:00
parent 1bdfd38ef5
commit 4ec56fd12f

View File

@ -16,12 +16,16 @@ export default function EmailVerifyPage() {
const [resendCooldown, setResendCooldown] = useState(0)
const [initialEmailSent, setInitialEmailSent] = useState(false)
const inputsRef = useRef<Array<HTMLInputElement | null>>([])
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
}
}