// components/auth/components-auth-register-form.tsx "use client"; import * as React from "react"; import { useRouter } from "next/navigation"; type Props = { redirectTo?: string; // optional override }; export default function ComponentsAuthRegisterForm({ redirectTo = "/dashboard" }: Props) { const router = useRouter(); const [email, setEmail] = React.useState(""); const [password, setPassword] = React.useState(""); const [confirm, setConfirm] = React.useState(""); const [loading, setLoading] = React.useState(false); const [error, setError] = React.useState(null); async function onSubmit(e: React.FormEvent) { e.preventDefault(); setError(null); if (!email.trim() || !password) { setError("Please fill in all fields."); return; } if (password.length < 8) { setError("Password must be at least 8 characters."); return; } if (password !== confirm) { setError("Passwords do not match."); return; } const API = process.env.NEXT_PUBLIC_FASTAPI_URL; // e.g. http://localhost:8000 try { setLoading(true); const res = await fetch(`${API}/auth/register`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password }), credentials: 'include', }); const data = await res.json(); if (!res.ok) { setError(data?.message || "Registration failed."); return; } // Cookie is set by API; just route away router.replace(redirectTo); } catch (err) { setError("Network error. Please try again."); } finally { setLoading(false); } } return (
setEmail(e.target.value)} disabled={loading} required />
setPassword(e.target.value)} disabled={loading} required minLength={8} />
setConfirm(e.target.value)} disabled={loading} required minLength={8} />
{error && (

{error}

)}
); }