"use client"; import { useState, type ReactNode } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { ArrowLeft, ArrowRight, Loader2, UserPlus } from "lucide-react"; import { useAuth } from "@/lib/auth-store"; import { http, toUserError } from "@/lib/api"; import type { User } from "@/lib/types"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { PasswordInput } from "@/components/ui/password-input"; import { Label } from "@/components/ui/label"; import { cn } from "@/lib/utils"; import { CaptchaField, type CaptchaChallenge } from "@/components/captcha-field"; import { validateEmail, validateName, validatePassword, clampPhoneInput, normalizePhone, PHONE_INPUT_MAX_LEN, validatePhone, validateSecurityQA, validateUsername, } from "@/lib/validation"; const STEPS = [ { id: 1, title: "هویت", hint: "نام، شناسه و تلفن" }, { id: 2, title: "امنیت", hint: "ایمیل و سوال امنیتی" }, { id: 3, title: "گذرواژه", hint: "رمز و کد امنیتی" }, ] as const; type RegisterFormProps = { endpoint: string; submitLabel: string; description?: string; }; function Field({ id, label, children, }: { id: string; label: string; children: ReactNode; }) { return (
{children}
); } export function RegisterForm({ endpoint, submitLabel, description }: RegisterFormProps) { const router = useRouter(); const setSession = useAuth((s) => s.setSession); const [step, setStep] = useState(1); const [name, setName] = useState(""); const [username, setUsername] = useState(""); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [passwordConfirm, setPasswordConfirm] = useState(""); const [phone, setPhone] = useState(""); const [securityQuestion, setSecurityQuestion] = useState(""); const [securityAnswer, setSecurityAnswer] = useState(""); const [captchaAnswer, setCaptchaAnswer] = useState(""); const [challenge, setChallenge] = useState(null); const [captchaKey, setCaptchaKey] = useState(0); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const goNext = () => { setError(null); if (step === 1) { const nErr = validateName(name); if (nErr) { setError(nErr); return; } const uErr = validateUsername(username); if (uErr) { setError(uErr); return; } const pErr = validatePhone(phone); if (pErr) { setError(pErr); return; } setStep(2); return; } if (step === 2) { const eErr = validateEmail(email); if (eErr) { setError(eErr); return; } const sErr = validateSecurityQA(securityQuestion, securityAnswer); if (sErr) { setError(sErr); return; } setStep(3); } }; const goBack = () => { setError(null); setStep((s) => Math.max(1, s - 1)); }; const submit = async (e: React.FormEvent) => { e.preventDefault(); if (step < 3) { goNext(); return; } setError(null); const pwErr = validatePassword(password, passwordConfirm); if (pwErr) { setError(pwErr); return; } if (!challenge) { setError("ابتدا کد امنیتی را بارگذاری کنید"); setCaptchaKey((k) => k + 1); return; } setLoading(true); try { const data = await http.post<{ accessToken: string; refreshToken: string; user: User }>( endpoint, { name: name.trim(), username: username.trim().toLowerCase(), email: email.trim().toLowerCase(), password, passwordConfirm, phone: normalizePhone(phone), securityQuestion: securityQuestion.trim(), securityAnswer: securityAnswer.trim(), challengeId: challenge.challengeId, captchaAnswer, }, ); setSession(data.accessToken, data.refreshToken, data.user); router.push("/cap"); } catch (err) { setError(toUserError(err, "ثبت‌نام ناموفق بود")); setCaptchaKey((k) => k + 1); } finally { setLoading(false); } }; const currentStep = STEPS[step - 1]; const progressPct = (step / STEPS.length) * 100; return (
{description && (

{description}

)}
گام {step} از {STEPS.length} {currentStep?.hint}
    {STEPS.map((s) => { const active = step === s.id; const done = step > s.id; return (
  1. {s.title}
  2. ); })}
void submit(ev)} className="space-y-4"> {step === 1 && ( <> setName(e.target.value)} autoComplete="name" autoFocus /> setUsername(e.target.value)} dir="ltr" className="text-left" autoComplete="username" /> setPhone(clampPhoneInput(e.target.value))} dir="ltr" className="text-left" placeholder="0912…" autoComplete="tel" inputMode="tel" maxLength={PHONE_INPUT_MAX_LEN} /> )} {step === 2 && ( <> setEmail(e.target.value)} dir="ltr" className="text-left" autoComplete="email" autoFocus /> setSecurityQuestion(e.target.value)} placeholder="مثلاً نام مدرسهٔ اول؟" /> setSecurityAnswer(e.target.value)} autoComplete="off" dir="rtl" placeholder="جواب سوال امنیتی" /> )} {step === 3 && ( <> setPassword(e.target.value)} minLength={8} autoComplete="new-password" autoFocus placeholder="حداقل ۸ نویسه" /> setPasswordConfirm(e.target.value)} minLength={8} autoComplete="new-password" placeholder="همان رمز را دوباره وارد کنید" /> )} {error &&

{error}

}
{step > 1 && ( )} {step < 3 ? ( ) : ( )}

حساب دارید؟{" "} ورود

); }