"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import Link from "next/link"; import { authAPI } from "@/lib/api"; import { useAuthStore } from "@/stores/authStore"; export default function RegisterPage() { const router = useRouter(); const setAuth = useAuthStore((s) => s.setAuth); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(""); const [loading, setLoading] = useState(false); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setError(""); if (password.length < 8) { setError("Password must be at least 8 characters."); return; } setLoading(true); try { const { access_token, user } = await authAPI.register(email.trim(), password); setAuth(access_token, { id: user.id, email: user.email, is_admin: user.is_admin }); router.replace("/"); } catch (err: unknown) { const msg = (err as { response?: { data?: { detail?: string } } })?.response?.data?.detail || "Registration failed. Please try again."; setError(msg); } finally { setLoading(false); } } return (
Create your account
Already have an account?{" "} Sign in