'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 (
{/* Tactical Header Badge */}

Verify Telemetry Signature

Enter the 6-digit cryptographic authorization code dispatched to your registered address.

{isLongToken && (
Cryptographic token detected from authentication link.
)} {/* Main Challenge Form */}
{isLongToken ? (
setToken(e.target.value)} required className="w-full px-4 py-3 bg-[#070a0f] border border-[#262a33] rounded-xl text-white font-mono text-sm focus:border-[#00f2fe] outline-none" />
) : (
{ setToken(val); setMessage(''); }} disabled={busy || isSuccess} />
)} {/* Primary Action Button */} {/* Status Message */} {message && (
{message}
)}
{/* Resend Code Section */}
{showEmailInput && (
setEmail(e.target.value)} className="flex-1 px-3 py-2 bg-[#070a0f] border border-[#262a33] rounded-lg text-white text-xs font-mono focus:border-[#00f2fe] outline-none" />
)}
Didn't receive the code? {resendCooldown > 0 ? ( Resend code in {resendCooldown}s ) : ( )}
← Return to sign in
); }