2026-06-20 19:13:05 +03:00
|
|
|
import { useState, type FormEvent } from "react";
|
|
|
|
|
import { useNavigate, Link } from "react-router-dom";
|
2026-06-19 11:28:04 +03:00
|
|
|
import { useTranslation } from "react-i18next";
|
2026-06-20 19:13:05 +03:00
|
|
|
import { useAuthStore } from "@/stores/authStore";
|
|
|
|
|
import { useToastStore } from "@/stores/toastStore";
|
2026-06-21 02:41:48 +03:00
|
|
|
import { ApiError, toErrorMessage } from "@/lib/api";
|
2026-06-19 11:28:04 +03:00
|
|
|
import { Button } from "@/components/ui/Button";
|
|
|
|
|
import { Input } from "@/components/ui/Input";
|
2026-06-20 19:13:05 +03:00
|
|
|
import { Card } from "@/components/ui/Card";
|
2026-06-19 11:28:04 +03:00
|
|
|
|
2026-06-21 02:41:48 +03:00
|
|
|
const USERNAME_INVALID_CHARS_RE = /[^a-zA-Z0-9_]/;
|
|
|
|
|
|
2026-06-19 11:28:04 +03:00
|
|
|
export function RegisterPage() {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const navigate = useNavigate();
|
2026-06-20 19:13:05 +03:00
|
|
|
const register = useAuthStore((s) => s.register);
|
|
|
|
|
const pushToast = useToastStore((s) => s.push);
|
|
|
|
|
|
2026-06-19 11:28:04 +03:00
|
|
|
const [email, setEmail] = useState("");
|
|
|
|
|
const [username, setUsername] = useState("");
|
|
|
|
|
const [password, setPassword] = useState("");
|
2026-06-20 19:13:05 +03:00
|
|
|
const [passwordConfirm, setPasswordConfirm] = useState("");
|
|
|
|
|
const [submitting, setSubmitting] = useState(false);
|
|
|
|
|
const [errors, setErrors] = useState<Record<string, string>>({});
|
|
|
|
|
|
|
|
|
|
const validate = (): boolean => {
|
|
|
|
|
const next: Record<string, string> = {};
|
|
|
|
|
if (!email.includes("@")) next.email = t("errors.validation");
|
|
|
|
|
if (username.trim().length < 3) next.username = t("errors.validation");
|
2026-06-21 02:41:48 +03:00
|
|
|
if (USERNAME_INVALID_CHARS_RE.test(username.trim())) {
|
|
|
|
|
next.username = t("auth.username_invalid_chars");
|
|
|
|
|
}
|
2026-06-20 19:13:05 +03:00
|
|
|
if (password.length < 8) next.password = t("errors.validation");
|
|
|
|
|
if (password !== passwordConfirm) next.password_confirm = t("errors.validation");
|
|
|
|
|
setErrors(next);
|
|
|
|
|
return Object.keys(next).length === 0;
|
|
|
|
|
};
|
2026-06-19 11:28:04 +03:00
|
|
|
|
2026-06-20 19:13:05 +03:00
|
|
|
const handleSubmit = async (e: FormEvent) => {
|
2026-06-19 11:28:04 +03:00
|
|
|
e.preventDefault();
|
2026-06-20 19:13:05 +03:00
|
|
|
if (!validate()) return;
|
|
|
|
|
setSubmitting(true);
|
2026-06-19 11:28:04 +03:00
|
|
|
try {
|
2026-06-20 19:13:05 +03:00
|
|
|
await register({
|
|
|
|
|
email: email.trim(),
|
|
|
|
|
username: username.trim(),
|
|
|
|
|
password,
|
|
|
|
|
password_confirm: passwordConfirm,
|
|
|
|
|
});
|
|
|
|
|
pushToast("success", t("auth.register_success"));
|
|
|
|
|
navigate("/login");
|
|
|
|
|
} catch (err) {
|
2026-06-21 02:41:48 +03:00
|
|
|
if (err instanceof ApiError && err.status === 422) {
|
|
|
|
|
// FastAPI 422 validation error — extract field-level messages.
|
|
|
|
|
const detail = err.details;
|
|
|
|
|
const fieldMsgs: Record<string, string> = {};
|
|
|
|
|
let generalMsg = "";
|
|
|
|
|
if (Array.isArray(detail)) {
|
|
|
|
|
for (const item of detail) {
|
|
|
|
|
if (item && typeof item === "object") {
|
|
|
|
|
const obj = item as { loc?: unknown; msg?: string; message?: string };
|
|
|
|
|
const loc = Array.isArray(obj.loc)
|
|
|
|
|
? obj.loc.map((x) => String(x)).filter((x) => x !== "body" && x !== "query").join(".")
|
|
|
|
|
: obj.loc != null
|
|
|
|
|
? String(obj.loc)
|
|
|
|
|
: "";
|
|
|
|
|
const msg = obj.msg || obj.message || "";
|
|
|
|
|
if (loc) {
|
|
|
|
|
fieldMsgs[loc] = msg || t("errors.validation");
|
|
|
|
|
} else {
|
|
|
|
|
generalMsg = generalMsg ? `${generalMsg}; ${msg}` : msg;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// If we have specific field messages, show them inline.
|
|
|
|
|
if (Object.keys(fieldMsgs).length > 0) {
|
|
|
|
|
setErrors((prev) => ({ ...prev, ...fieldMsgs }));
|
|
|
|
|
}
|
|
|
|
|
// If the username field failed validation, show the specific message.
|
|
|
|
|
if (fieldMsgs.username || fieldMsgs["username"]) {
|
|
|
|
|
pushToast("error", t("auth.username_invalid_chars"));
|
|
|
|
|
} else {
|
|
|
|
|
pushToast("error", generalMsg || t("auth.register_failed"));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
pushToast("error", toErrorMessage(err, t("auth.register_failed")));
|
|
|
|
|
}
|
2026-06-19 11:28:04 +03:00
|
|
|
} finally {
|
2026-06-20 19:13:05 +03:00
|
|
|
setSubmitting(false);
|
2026-06-19 11:28:04 +03:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2026-06-20 19:13:05 +03:00
|
|
|
<div className="mx-auto flex min-h-[calc(100vh-3.5rem)] max-w-md items-center p-4">
|
|
|
|
|
<Card className="w-full" title={t("auth.register_title")}>
|
|
|
|
|
<form onSubmit={handleSubmit} className="space-y-3">
|
|
|
|
|
<Input
|
|
|
|
|
label={t("auth.email")}
|
|
|
|
|
type="email"
|
|
|
|
|
value={email}
|
|
|
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
|
|
|
autoComplete="email"
|
|
|
|
|
required
|
|
|
|
|
error={errors.email}
|
|
|
|
|
/>
|
|
|
|
|
<Input
|
|
|
|
|
label={t("auth.username")}
|
|
|
|
|
value={username}
|
|
|
|
|
onChange={(e) => setUsername(e.target.value)}
|
|
|
|
|
autoComplete="username"
|
|
|
|
|
required
|
|
|
|
|
error={errors.username}
|
2026-06-21 02:41:48 +03:00
|
|
|
hint={t("auth.username_hint")}
|
2026-06-20 19:13:05 +03:00
|
|
|
/>
|
|
|
|
|
<Input
|
|
|
|
|
label={t("auth.password")}
|
|
|
|
|
type="password"
|
|
|
|
|
value={password}
|
|
|
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
|
|
|
autoComplete="new-password"
|
|
|
|
|
required
|
|
|
|
|
error={errors.password}
|
|
|
|
|
/>
|
|
|
|
|
<Input
|
|
|
|
|
label={t("auth.password_confirm")}
|
|
|
|
|
type="password"
|
|
|
|
|
value={passwordConfirm}
|
|
|
|
|
onChange={(e) => setPasswordConfirm(e.target.value)}
|
|
|
|
|
autoComplete="new-password"
|
|
|
|
|
required
|
|
|
|
|
error={errors.password_confirm}
|
|
|
|
|
/>
|
|
|
|
|
<Button type="submit" loading={submitting} fullWidth>
|
|
|
|
|
{t("auth.register_button")}
|
|
|
|
|
</Button>
|
|
|
|
|
<p className="text-center text-sm text-fg-muted">
|
|
|
|
|
<Link to="/login" className="text-accent hover:underline">
|
2026-06-19 11:28:04 +03:00
|
|
|
{t("auth.have_account")}
|
|
|
|
|
</Link>
|
|
|
|
|
</p>
|
2026-06-20 19:13:05 +03:00
|
|
|
</form>
|
2026-06-19 11:28:04 +03:00
|
|
|
</Card>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|