"use client"; import { useState } from "react"; import { useMutation } from "@tanstack/react-query"; import { RECOVERY_CODE_LENGTH } from "@/lib/constants"; export default function RecoverPage() { const [code, setCode] = useState(""); const claimMutation = useMutation({ mutationFn: async (recoveryCode: string) => { const res = await fetch("/api/auth/recovery/claim", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ code: recoveryCode }), }); const data = (await res.json()) as { ok?: boolean; error?: string; }; if (!res.ok) { throw new Error(data.error || "Recovery failed"); } return data as { ok: true }; }, onSuccess: () => { // Full document load so the new auth cookie set by the server is sent // with the next request (router.push() keeps the SPA navigation in the // existing fetch context and may race the cookie write). window.location.assign("/"); }, }); function handleSubmit(e: React.FormEvent) { e.preventDefault(); const trimmed = code.trim(); if (trimmed.length === 0) return; claimMutation.mutate(trimmed); } return ( <>

Recover Your Account

setCode(e.target.value)} maxLength={RECOVERY_CODE_LENGTH} placeholder="Enter your 24-character code" className="w-full rounded-lg border border-foreground/20 bg-background px-3 py-2 font-mono text-foreground placeholder:text-foreground/40 focus:outline-none focus:ring-2 focus:ring-blue-500" autoFocus autoComplete="off" spellCheck={false} />
{claimMutation.error && (

{claimMutation.error instanceof Error ? claimMutation.error.message : "Something went wrong."}

)}

Don't have an account?{" "} Create one

); }