import { useState } from "react"; import { Link, useNavigate } from "react-router-dom"; import { registerUser, loginUser, getMe, setClientTokens } from "../api/client"; import { useAuthStore } from "../stores/auth"; interface FormState { full_name: string; email: string; password: string; } interface Errors { full_name?: string; email?: string; password?: string; global?: string; } export default function RegisterPage() { const navigate = useNavigate(); const login = useAuthStore((s) => s.login); const isAuthenticated = useAuthStore((s) => s.isAuthenticated); const [form, setForm] = useState({ full_name: "", email: "", password: "", }); const [errors, setErrors] = useState({}); const [isLoading, setIsLoading] = useState(false); if (isAuthenticated) { navigate("/", { replace: true }); return null; } function validate(): boolean { const errs: Errors = {}; if (!form.full_name.trim() || form.full_name.trim().length < 2) errs.full_name = "Nom complet requis (2 caractères minimum)"; if (!form.email) errs.email = "Email requis"; else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(form.email)) errs.email = "Email invalide"; if (!form.password || form.password.length < 8) errs.password = "Mot de passe requis (8 caractères minimum)"; setErrors(errs); return Object.keys(errs).length === 0; } async function handleSubmit(e: React.FormEvent) { e.preventDefault(); if (!validate()) return; setIsLoading(true); setErrors({}); try { await registerUser(form.email, form.password, form.full_name.trim()); // Auto-login after registration const tokens = await loginUser(form.email, form.password); setClientTokens(tokens.access_token, tokens.refresh_token); const user = await getMe(); login(user, tokens.access_token, tokens.refresh_token); navigate("/", { replace: true }); } catch (err) { const isConflict = err instanceof Error && err.message.toLowerCase().includes("409"); setErrors({ global: isConflict ? "Cet email est déjà utilisé" : "Erreur lors de l'inscription, veuillez réessayer", }); } finally { setIsLoading(false); } } return (

Budget Tracker

Créez votre compte

{errors.global && (
{errors.global}
)}
setForm((f) => ({ ...f, full_name: e.target.value })) } className={`w-full rounded-lg border px-3 py-2 text-sm outline-none focus:ring-2 focus:ring-primary/30 ${ errors.full_name ? "border-red-400" : "border-slate-200" }`} placeholder="Jean Dupont" /> {errors.full_name && (

{errors.full_name}

)}
setForm((f) => ({ ...f, email: e.target.value })) } className={`w-full rounded-lg border px-3 py-2 text-sm outline-none focus:ring-2 focus:ring-primary/30 ${ errors.email ? "border-red-400" : "border-slate-200" }`} placeholder="vous@exemple.com" /> {errors.email && (

{errors.email}

)}
setForm((f) => ({ ...f, password: e.target.value })) } className={`w-full rounded-lg border px-3 py-2 text-sm outline-none focus:ring-2 focus:ring-primary/30 ${ errors.password ? "border-red-400" : "border-slate-200" }`} placeholder="8 caractères minimum" /> {errors.password && (

{errors.password}

)}

Déjà un compte ?{" "} Se connecter

); }