"use client"; import { useState, type FormEvent } from "react"; import { useRouter, useSearchParams } from "next/navigation"; import { ResendVerificationButton } from "./ResendVerificationButton"; import { TextField, FormError } from "@/components/FormField"; import { safeLoginRedirect } from "@/lib/auth/redirect"; import { TurnstileWidget } from "@/components/TurnstileWidget"; export function LoginForm({ siteKey }: { siteKey: string }) { const router = useRouter(); const searchParams = useSearchParams(); 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 email = String(data.get("email") ?? ""); const password = String(data.get("password") ?? ""); try { const res = await fetch("/api/customers/login", { method: "POST", headers: { "Content-Type": "application/json" }, credentials: "include", body: JSON.stringify({ email, password, token }), }); if (!res.ok) { const body = await res.json().catch(() => null); setError(body?.error || "E-Mail oder Passwort ist nicht korrekt."); setLoading(false); return; } router.push(safeLoginRedirect(searchParams.get("next"))); router.refresh(); } catch { setError("Verbindung fehlgeschlagen. Bitte versuche es erneut."); setLoading(false); } finally { setResetKey((key) => key + 1); } } return (
{searchParams.get("registered") === "1" && (

Dein Konto wurde erstellt. Melde dich jetzt an.

)} {error && {error}} ); }