"use client" import { useEffect, useState } from "react" import Link from "next/link" import { useSearchParams } from "next/navigation" import { CheckCircle2, XCircle } from "lucide-react" type Status = "verifying" | "success" | "error" export function VerifyEmailForm() { const searchParams = useSearchParams() const token = searchParams.get("token") // ?kind=secondary (satt av send_secondary_email_verification, app/email.py) // skiller "legg til ekstra adresse" fra det vanlige "bytt hovedadresse"- // løpet -- samme sides UI, ulikt endepunkt/budskap. const isSecondary = searchParams.get("kind") === "secondary" const [status, setStatus] = useState(token ? "verifying" : "error") const [email, setEmail] = useState(null) const [error, setError] = useState(null) useEffect(() => { if (!token) { setError("Mangler kode i lenken.") return } async function verify() { try { const res = await fetch(isSecondary ? "/auth/secondary-email/confirm" : "/auth/profile/email/confirm", { method: "POST", headers: { "Content-Type": "application/json" }, credentials: "include", body: JSON.stringify({ token }), }) if (!res.ok) { const body = await res.json().catch(() => null) throw new Error(body?.detail?.message ?? "Lenken er ugyldig eller utløpt.") } if (!isSecondary) { const result: { email: string } = await res.json() setEmail(result.email) } setStatus("success") } catch (err) { setError(err instanceof Error ? err.message : "Noe gikk galt. Prøv igjen.") setStatus("error") } } void verify() }, [token, isSecondary]) if (status === "verifying") { return (
) } if (status === "success") { return (

{isSecondary ? "Adresse lagt til" : "E-post oppdatert"}

{isSecondary ? (

Adressen er nå knyttet til kontoen din — du kan logge inn med den i tillegg til hovedadressen.

) : ( email && (

Du logger nå inn med {email}.

) )}
Til kontoinnstillinger
) } return (

Kunne ikke bekrefte

{error}

Til kontoinnstillinger
) }