"use client"; import { useState, type FormEvent } from "react"; import { useRouter } from "next/navigation"; import Link from "next/link"; import { Container } from "@/components/layout/Container"; import { Input } from "@/components/ui/Input"; import { Button } from "@/components/ui/Button"; import { api } from "@/lib/api"; const MIN_PASSWORD_LENGTH = 8; export default function RegisterPage() { const router = useRouter(); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); const [success, setSuccess] = useState(false); const passwordValid = password.length >= MIN_PASSWORD_LENGTH; const handleSubmit = async (e: FormEvent) => { e.preventDefault(); setError(null); if (!passwordValid) { setError(`Password must be at least ${MIN_PASSWORD_LENGTH} characters`); return; } setLoading(true); try { await api.register({ email, password }); setSuccess(true); setTimeout(() => { router.push("/login"); }, 1500); } catch (err) { if (err instanceof Error) { if (err.message.includes("409") || err.message.toLowerCase().includes("already")) { setError("An account with this email already exists"); } else { setError(err.message); } } else { setError("Registration failed"); } } finally { setLoading(false); } }; if (success) { return (

Account Created

Redirecting to login...

); } return (

Register

setEmail(e.target.value)} placeholder="you@example.com" required error={error ? " " : undefined} />
setPassword(e.target.value)} placeholder="••••••••" required className={`w-full border-b py-3 bg-transparent text-black placeholder:text-neutral-400 focus:outline-none transition-colors ${ error ? "border-red-500 animate-shake" : password.length > 0 ? passwordValid ? "border-green-500" : "border-yellow-500" : "border-neutral-200 focus:border-black" }`} /> {password.length}/{MIN_PASSWORD_LENGTH}
{error &&

{error}

}

Already have an account?{" "} Sign in

); }