Files
kai 8d88e90a07
release / which version (push) Successful in 3s
release / olares chart · sanity (push) Successful in 4s
release / app (push) Successful in 1m2s
release / tag and release (push) Successful in 2s
26.11.53: Mails zur Runde (#55)
2026-11-09 07:38:56 +00:00

85 lines
3.7 KiB
TypeScript

"use client";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { useTranslations } from "next-intl";
import { useEffect, useState } from "react";
import { Button } from "@/components/ui/button";
import { ErrorNotice } from "@/components/ui/notice";
type Purpose = "login" | "verify" | "notify";
type State = { kind: "ready"; purpose: Purpose; token: string } | { kind: "expired" } | { kind: "verified"; purpose: Purpose } | { kind: "loading" };
// Der Link aus der Mail (26.11.40): Der Token steht hinter # und geht nie an den
// Server, bis die Person tippt — ein Mail-Scanner, der nur abruft, verbraucht ihn nicht.
export function MailLink() {
const t = useTranslations("auth");
const router = useRouter();
const [state, setState] = useState<State>({ kind: "loading" });
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
const m = window.location.hash.match(/^#(login|verify|notify)=([A-Za-z0-9_-]{20,100})$/);
// Aus der Adresszeile nehmen: kein Token im Verlauf, kein zweites Einlösen beim Neuladen.
history.replaceState(null, "", window.location.pathname);
setState(m ? { kind: "ready", purpose: m[1] as Purpose, token: m[2]! } : { kind: "expired" });
}, []);
async function go() {
if (state.kind !== "ready") return;
setBusy(true);
setError(null);
const res = await fetch("/api/auth/mail/verify", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ token: state.token }),
}).catch(() => null);
setBusy(false);
if (res?.ok) {
const { purpose } = await res.json();
if (purpose === "login") router.replace("/");
else setState({ kind: "verified", purpose });
return;
}
if (res?.status === 410) return setState({ kind: "expired" });
setError(res?.status === 429 ? "tooMany" : "unknown");
}
if (state.kind === "loading") return null;
if (state.kind === "expired") {
return (
<div className="flex flex-col items-center gap-4 text-center">
<h1 className="text-balance text-4xl leading-tight">{t("mailLink.expiredTitle")}</h1>
<p className="mx-auto max-w-[34ch] text-balance text-muted-foreground">{t("mailLink.expiredIntro")}</p>
<Button asChild size="lg" className="self-stretch">
<Link href="/anmelden">{t("mailLink.back")}</Link>
</Button>
</div>
);
}
if (state.kind === "verified") {
return (
<div className="flex flex-col items-center gap-4 text-center">
<h1 className="text-balance text-4xl leading-tight">{t(state.purpose === "notify" ? "mailLink.notifyDone" : "mailLink.verified")}</h1>
{state.purpose === "notify" ? <p className="mx-auto max-w-[34ch] text-balance text-muted-foreground">{t("mailLink.notifyDoneIntro")}</p> : null}
<Button asChild size="lg" className="self-stretch">
<Link href={state.purpose === "notify" ? "/" : "/mehr"}>{t("mailLink.toMore")}</Link>
</Button>
</div>
);
}
const login = state.purpose === "login";
const key = state.purpose === "notify" ? "notify" : login ? "login" : "verify";
return (
<div className="flex flex-col items-center gap-4 text-center">
<h1 className="text-balance text-4xl leading-tight">{t(`mailLink.${key}Title`)}</h1>
<p className="mx-auto max-w-[34ch] text-balance text-muted-foreground">{t(`mailLink.${key}Intro`)}</p>
{error ? <ErrorNotice className="self-stretch">{t(`errors.${error}`)}</ErrorNotice> : null}
<Button size="lg" className="self-stretch" onClick={go} disabled={busy}>
{busy ? t("working") : t(`mailLink.${key}Submit`)}
</Button>
</div>
);
}