"use client"; import { useState, type FormEvent } from "react"; import { useRouter } from "next/navigation"; import { TextField, FormError } from "@/components/FormField"; import { TurnstileWidget } from "@/components/TurnstileWidget"; export function RegisterForm({ siteKey }: { siteKey: string }) { const router = useRouter(); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); const [token, setToken] = useState(null); const [resetKey, setResetKey] = useState(0); async function handleSubmit(e: FormEvent) { e.preventDefault(); setError(null); setLoading(true); const data = new FormData(e.currentTarget); const name = String(data.get("name") ?? ""); const email = String(data.get("email") ?? ""); const password = String(data.get("password") ?? ""); try { const createRes = await fetch("/api/customers", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name, email, password, token }), }); if (!createRes.ok) { const body = await createRes.json().catch(() => null); setError(body?.error || body?.errors?.[0]?.message || "Die Registrierung ist fehlgeschlagen. Ist die E-Mail-Adresse schon vergeben?"); setLoading(false); return; } // Login is separately protected by its own single-use Turnstile token. router.push("/login?registered=1"); } catch { setError("Verbindung fehlgeschlagen. Bitte versuche es erneut."); setLoading(false); } finally { setResetKey((key) => key + 1); } } return (
{error && {error}} ); }