"use client"; import { FormEvent, useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import Link from "next/link"; import { useAppLocale } from "@/features/i18n/LocaleProvider"; import { getAuthMessages } from "@/features/i18n/messages/auth"; type PasswordStrengthLabel = "weak" | "medium" | "strong"; function getPasswordStrengthLabel(password: string): PasswordStrengthLabel | null { if (!password || password.length < 4) { return null; } let score = 0; if (/[a-z]/.test(password)) score += 1; if (/[A-Z]/.test(password)) score += 1; if (/[0-9]/.test(password)) score += 1; if (/[^a-zA-Z0-9]/.test(password)) score += 1; if (password.length >= 12) score += 1; if (password.length < 8 || score <= 2) { return "weak"; } if (score === 3) { return "medium"; } return "strong"; } export default function SignupPage() { const router = useRouter(); const locale = useAppLocale(); const { signup: t } = getAuthMessages(locale); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [passwordConfirm, setPasswordConfirm] = useState(""); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); useEffect(() => { let cancelled = false; const checkAuth = async () => { try { const res = await fetch("/api/auth/me"); if (!cancelled && res.ok) { router.push("/dashboard"); } } catch { // ignore } }; void checkAuth(); return () => { cancelled = true; }; }, [router]); const handleSubmit = async (e: FormEvent) => { e.preventDefault(); setError(null); if (password !== passwordConfirm) { setError(t.mismatchError); return; } setLoading(true); try { const res = await fetch("/api/auth/signup", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email, password }), }); if (!res.ok) { try { const data = await res.json(); setError(data?.message ?? t.signupFailedFallbackError); } catch { setError(t.signupFailedFallbackError); } setLoading(false); return; } router.push("/dashboard"); } catch { setError(t.errorNetwork); } finally { setLoading(false); } }; const strengthLabel = getPasswordStrengthLabel(password); let strengthText: string | null = null; if (strengthLabel) { strengthText = strengthLabel === "weak" ? t.passwordStrengthWeak : strengthLabel === "medium" ? t.passwordStrengthMedium : t.passwordStrengthStrong; } return (

{t.title}

{t.description}

{error &&

{error}

}
setEmail(e.target.value)} className="w-full rounded border border-slate-300 bg-white px-2 py-1 text-xs text-slate-900 focus:outline-none focus:ring-1 focus:ring-sky-500 dark:border-slate-700 dark:bg-slate-950 dark:text-slate-50" required />
setPassword(e.target.value)} className="w-full rounded border border-slate-300 bg-white px-2 py-1 text-xs text-slate-900 focus:outline-none focus:ring-1 focus:ring-sky-500 dark:border-slate-700 dark:bg-slate-950 dark:text-slate-50" required minLength={8} />
{strengthLabel && (

{t.passwordStrengthPrefix} {strengthText}

)}
setPasswordConfirm(e.target.value)} className="w-full rounded border border-slate-300 bg-white px-2 py-1 text-xs text-slate-900 focus:outline-none focus:ring-1 focus:ring-sky-500 dark:border-slate-700 dark:bg-slate-950 dark:text-slate-50" required minLength={8} />

{t.loginPromptPrefix} {" "} {t.loginLinkText} {t.loginPromptSuffix && " "} {t.loginPromptSuffix}

); }