import { useState } from "react"; import { useTranslation } from "react-i18next"; import { Link, useNavigate } from "react-router-dom"; import { useAuthStore } from "@/stores/auth-store"; import { register } from "@/api/auth"; export function RegisterForm() { const { t } = useTranslation(); const navigate = useNavigate(); const setAuth = useAuthStore((s) => s.setAuth); const [email, setEmail] = useState(""); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [fullName, setFullName] = useState(""); const [error, setError] = useState(""); const [loading, setLoading] = useState(false); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(""); if (password.length < 8) { setError(t("auth.errors.passwordMinLength")); return; } if (password !== confirmPassword) { setError(t("auth.errors.passwordMismatch")); return; } if (!/^[a-zA-Z0-9_-]{3,50}$/.test(username)) { setError(t("auth.errors.usernameFormat")); return; } setLoading(true); try { const data = await register(email, username, password, fullName || undefined); setAuth(data.user, data.access_token, data.refresh_token); navigate("/"); } catch (err: unknown) { const msg = (err as { response?: { data?: { detail?: string } } })?.response?.data?.detail || t("auth.errors.emailExists"); setError(msg); } finally { setLoading(false); } }; return (
); }