"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import Link from "next/link"; import { register } from "@/lib/api"; import { useAuth } from "@/lib/auth"; import { useTranslation } from "@/lib/i18n"; function getPasswordStrength(pw: string): { level: number; label: string; color: string } { if (!pw) return { level: 0, label: "", color: "" }; let score = 0; if (pw.length >= 6) score++; if (pw.length >= 10) score++; if (/[A-Z]/.test(pw)) score++; if (/[0-9]/.test(pw)) score++; if (/[^A-Za-z0-9]/.test(pw)) score++; if (score <= 1) return { level: 1, label: "weak", color: "bg-red-500" }; if (score <= 2) return { level: 2, label: "fair", color: "bg-orange-500" }; if (score <= 3) return { level: 3, label: "good", color: "bg-yellow-500" }; if (score <= 4) return { level: 4, label: "strong", color: "bg-green-500" }; return { level: 5, label: "excellent", color: "bg-emerald-500" }; } export default function RegisterPage() { const [email, setEmail] = useState(""); const [name, setName] = useState(""); const [phone, setPhone] = useState(""); const [password, setPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [showPassword, setShowPassword] = useState(false); const [loading, setLoading] = useState(false); const [error, setError] = useState(""); const { setAuth } = useAuth(); const { t } = useTranslation(); const router = useRouter(); const strength = getPasswordStrength(password); const passwordsMatch = password === confirmPassword; async function handleSubmit(e: React.FormEvent) { e.preventDefault(); if (!email.trim() || !name.trim()) { setError(t("common.error")); return; } if (!password || password.length < 6) { setError(t("auth.passwordMinLength")); return; } if (password !== confirmPassword) { setError(t("auth.passwordMismatch")); return; } setLoading(true); setError(""); try { const result = await register({ email: email.trim(), name: name.trim(), phone: phone.trim() || undefined, password: password, }); setAuth(result.data.token, result.data.user); router.push("/tasks"); } catch (err) { setError(err instanceof Error ? err.message : t("common.error")); } finally { setLoading(false); } } return (

{t("auth.register")}

{error && (
{error}
)}
setName(e.target.value)} className="w-full px-3 py-2.5 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 focus:ring-2 focus:ring-blue-500 focus:border-transparent outline-none" autoFocus />
setEmail(e.target.value)} className="w-full px-3 py-2.5 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 focus:ring-2 focus:ring-blue-500 focus:border-transparent outline-none" autoComplete="email" />
setPhone(e.target.value)} className="w-full px-3 py-2.5 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 focus:ring-2 focus:ring-blue-500 focus:border-transparent outline-none" placeholder={t("auth.phonePlaceholder")} />
setPassword(e.target.value)} className="w-full px-3 py-2.5 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 focus:ring-2 focus:ring-blue-500 focus:border-transparent outline-none pr-10" placeholder="******" autoComplete="new-password" />
{password && (
{[1, 2, 3, 4, 5].map((i) => (
))}

{t(`auth.strength.${strength.label}`)}

)}

{t("auth.passwordMinLength")}

setConfirmPassword(e.target.value)} className={`w-full px-3 py-2.5 border rounded-lg bg-white dark:bg-gray-800 focus:ring-2 focus:ring-blue-500 focus:border-transparent outline-none ${ confirmPassword && !passwordsMatch ? "border-red-400 dark:border-red-600" : "border-gray-300 dark:border-gray-600" }`} placeholder="******" autoComplete="new-password" /> {confirmPassword && !passwordsMatch && (

{t("auth.passwordMismatch")}

)}

{t("auth.hasAccount")}{" "} {t("auth.submit")}

); }