feat: Prevent double email sending by adding a ref and reset on failure in EmailVerifyPage
This commit is contained in:
parent
1bdfd38ef5
commit
4ec56fd12f
@ -16,12 +16,16 @@ export default function EmailVerifyPage() {
|
|||||||
const [resendCooldown, setResendCooldown] = useState(0)
|
const [resendCooldown, setResendCooldown] = useState(0)
|
||||||
const [initialEmailSent, setInitialEmailSent] = useState(false)
|
const [initialEmailSent, setInitialEmailSent] = useState(false)
|
||||||
const inputsRef = useRef<Array<HTMLInputElement | null>>([])
|
const inputsRef = useRef<Array<HTMLInputElement | null>>([])
|
||||||
|
const emailSentRef = useRef(false) // Prevent double email sending
|
||||||
|
|
||||||
// Send verification email automatically on page load
|
// Send verification email automatically on page load
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!token || initialEmailSent) return
|
if (!token || initialEmailSent || emailSentRef.current) return
|
||||||
|
|
||||||
const sendInitialEmail = async () => {
|
const sendInitialEmail = async () => {
|
||||||
|
// Set the ref immediately to prevent race conditions
|
||||||
|
emailSentRef.current = true
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${process.env.NEXT_PUBLIC_API_BASE_URL}/api/send-verification-email`, {
|
const response = await fetch(`${process.env.NEXT_PUBLIC_API_BASE_URL}/api/send-verification-email`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@ -38,9 +42,13 @@ export default function EmailVerifyPage() {
|
|||||||
setResendCooldown(30) // Start cooldown after initial send
|
setResendCooldown(30) // Start cooldown after initial send
|
||||||
} else {
|
} else {
|
||||||
console.error('Failed to send initial verification email:', data.message)
|
console.error('Failed to send initial verification email:', data.message)
|
||||||
|
// Reset ref on failure so it can be retried
|
||||||
|
emailSentRef.current = false
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error sending initial verification email:', error)
|
console.error('Error sending initial verification email:', error)
|
||||||
|
// Reset ref on failure so it can be retried
|
||||||
|
emailSentRef.current = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user