feat(auth): add stepped auth and password recovery flows

This commit is contained in:
2026-05-03 17:10:02 +03:30
parent 9b1cd772fb
commit 380b794ab1
19 changed files with 1857 additions and 687 deletions

View File

@@ -0,0 +1,108 @@
import { Loader2 } from "lucide-react"
import { useMemo, useState } from "react"
import { Link, useNavigate } from "react-router-dom"
import { toast } from "sonner"
import { sendOtp } from "../../api/users"
import { Button } from "../../components/ui/button"
import { Input } from "../../components/ui/input"
import { useAuthFlow } from "../../context/AuthFlowContext"
import { useTranslation } from "../../hooks/useTranslation"
import { AuthPanel } from "./AuthPanel"
import { formatCooldown, getApiErrorMessage, handleThrottleError } from "./utils"
export function SignupMobilePage() {
const navigate = useNavigate()
const { t, lang } = useTranslation()
const { state, setMobile, setCode, setCooldown, clearCooldown } = useAuthFlow()
const isRtl = lang === "fa"
const [loading, setLoading] = useState(false)
const alert = useMemo(() => {
if (state.cooldowns.signupOtpSend <= 0) {
return null
}
const formatted = formatCooldown(state.cooldowns.signupOtpSend, isRtl)
return {
title: t.login.throttle.title,
description: t.login.throttle.otpSendMessage(formatted),
}
}, [isRtl, state.cooldowns.signupOtpSend, t.login.throttle])
const cooldownLabel =
state.cooldowns.signupOtpSend > 0
? t.login.throttle.countdownLabel(formatCooldown(state.cooldowns.signupOtpSend, isRtl))
: null
const handleContinue = async () => {
if (!state.signup.mobile) {
toast.error(t.login.toasts.enterMobile)
return
}
setLoading(true)
try {
await sendOtp(state.signup.mobile, "register")
clearCooldown("signupOtpSend")
setCode("signup", "")
navigate("/auth/signup/verify")
toast.success(t.login.toasts.verifySent)
} catch (error) {
if (
!handleThrottleError({
error,
cooldownKey: "signupOtpSend",
setCooldown,
formatTime: (seconds) => formatCooldown(seconds, isRtl),
throttleCopy: t.login.throttle,
})
) {
toast.error(getApiErrorMessage(error, t.login.toasts.failedOtp))
}
} finally {
setLoading(false)
}
}
return (
<AuthPanel
title={t.login.signupTitle}
description={t.login.signupDescription}
alert={alert}
>
<div className="grid gap-4">
<Input
id="signup-mobile"
placeholder={t.login.mobilePlaceholder}
type="tel"
dir="ltr"
maxLength={11}
disabled={loading}
value={state.signup.mobile}
onChange={(event) => setMobile("signup", event.target.value)}
className={`h-11 ${isRtl ? "text-end" : "text-start"}`}
/>
<Button
onClick={handleContinue}
disabled={loading || state.cooldowns.signupOtpSend > 0}
className="h-11 w-full"
>
{loading && <Loader2 className="me-2 h-4 w-4 animate-spin" />}
{cooldownLabel || t.login.sendSignupCode}
</Button>
<div className="text-center text-sm text-slate-500 dark:text-slate-400">
{t.login.haveAccount}{" "}
<Link
to="/auth/login"
className="font-medium text-blue-600 transition-colors hover:text-blue-500 dark:text-blue-400 dark:hover:text-blue-300"
>
{t.login.signIn}
</Link>
</div>
</div>
</AuthPanel>
)
}