'use client'; import { FormEvent, useEffect, useState } from 'react'; import Link from 'next/link'; import { SegmentedOtpInput } from './segmented-otp-input'; export function VerifyEmailForm() { const [token, setToken] = useState(''); const [email, setEmail] = useState(''); const [showEmailInput, setShowEmailInput] = useState(false); const [message, setMessage] = useState(''); const [isSuccess, setIsSuccess] = useState(false); const [busy, setBusy] = useState(false); const [resendCooldown, setResendCooldown] = useState(60); const [resendBusy, setResendBusy] = useState(false); const [isLongToken, setIsLongToken] = useState(false); useEffect(() => { const searchToken = new URLSearchParams(window.location.search).get('token') ?? ''; if (searchToken) { setToken(searchToken); if (searchToken.length !== 6) { setIsLongToken(true); } } }, []); // Cooldown countdown timer useEffect(() => { if (resendCooldown <= 0) return; const timer = setInterval(() => { setResendCooldown((prev) => Math.max(0, prev - 1)); }, 1000); return () => clearInterval(timer); }, [resendCooldown]); async function submit(event?: FormEvent) { if (event) event.preventDefault(); if (!token.trim()) return; setBusy(true); setMessage(''); setIsSuccess(false); try { const response = await fetch('/api/auth/verify-email', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ token: token.trim() }), }); const body = await response.json().catch(() => ({})); if (!response.ok) { throw new Error(body.detail ?? 'Unable to verify email signature.'); } setIsSuccess(true); setMessage('Telemetry signature verified successfully. Your account is fully active.'); } catch (error) { setIsSuccess(false); setMessage(error instanceof Error ? error.message : 'Unable to verify email.'); } finally { setBusy(false); } } async function handleResendCode() { if (!email.trim() && !showEmailInput) { setShowEmailInput(true); return; } if (!email.trim()) { setMessage('Please enter your registered email address to receive a new code.'); return; } setResendBusy(true); setMessage(''); try { const response = await fetch('/api/auth/verify-email-request', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ email: email.trim() }), }); const body = await response.json().catch(() => ({})); if (!response.ok) { throw new Error(body.detail ?? 'Unable to resend verification code.'); } setResendCooldown(60); setShowEmailInput(false); setMessage('Fresh verification code dispatched to your registered address.'); } catch (error) { setMessage(error instanceof Error ? error.message : 'Unable to resend verification code.'); } finally { setResendBusy(false); } } return (
Enter the 6-digit cryptographic authorization code dispatched to your registered address.