"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 ( <>
Don't have an account?{" "} Create one
> ); }