import React, { useState } from "react" import { useNavigate, Link } from "react-router-dom" import { Button } from "../components/ui/button" import { Input } from "../components/ui/input" import { SettingsMenu } from "../components/SettingsMenu" import { ArrowLeft, ArrowRight, Command, Loader2, Eye, EyeOff } from "lucide-react" import { toast } from "sonner" import { useTranslation } from "../hooks/useTranslation" import { loginWithPassword, sendOtp, loginWithOtp } from "../api/users" type AuthStep = "mobile" | "password" | "otp" type AuthMode = "login" | "register" export default function Auth() { const navigate = useNavigate() const { t, lang } = useTranslation() const isRtl = lang === "fa" const [step, setStep] = useState("mobile") const [mode, setMode] = useState("login") const [mobile, setMobile] = useState("") const [password, setPassword] = useState("") const [otpCode, setOtpCode] = useState("") const [loading, setLoading] = useState(false) const [showPassword, setShowPassword] = useState(false) // Added state for password visibility const handleTokenResponse = (access: string, refresh: string) => { localStorage.setItem("accessToken", access) localStorage.setItem("refreshToken", refresh) toast.success(t.login.toasts.successLogin) navigate("/profile") } const handleSendOtp = async (selectedMode: AuthMode) => { if (!mobile) return toast.error(t.login.toasts.enterMobile) setLoading(true) try { await sendOtp(mobile, selectedMode) setMode(selectedMode) setStep("otp") toast.success(t.login.toasts.verifySent) } catch (err: any) { toast.error(t.login.toasts.failedOtp) } finally { setLoading(false) } } const handlePasswordLogin = async (e: React.FormEvent) => { e.preventDefault() if (!mobile || !password) return toast.error(t.login.toasts.fillAll) setLoading(true) try { const data = await loginWithPassword(mobile, password) handleTokenResponse(data.access, data.refresh) } catch (err: any) { toast.error(t.login.toasts.invalidCreds) } finally { setLoading(false) } } const handleOtpVerify = async (e: React.FormEvent) => { e.preventDefault() if (!mobile || !otpCode) return toast.error(t.login.toasts.enterOtp) setLoading(true) try { const data = await loginWithOtp(mobile, otpCode) handleTokenResponse(data.access, data.refresh) } catch (err: any) { toast.error(t.login.toasts.invalidOtp) } finally { setLoading(false) } } const BackIcon = isRtl ? ArrowRight : ArrowLeft return (
{t.title || "Qlockify"}

"{t.login.brandingQuote}"

{step === "mobile" && t.login.welcome(t.title)} {step === "password" && t.login.enterPassword} {step === "otp" && t.login.verifyNumber}

{step === "mobile" && t.login.enterMobileDesc} {step === "password" && t.login.signInDesc} {step === "otp" && t.login.sentCodeDesc(mobile)}

{step === "mobile" && (
setMobile(e.target.value)} maxLength={11} disabled={loading} className={`h-11 ${isRtl ? "text-end" : "text-start"}`} />
{t.login.orContinueWith}
)} {step === "password" && (
setPassword(e.target.value)} disabled={loading} className={`h-11 pr-10 ${isRtl ? "text-end" : "text-start"}`} />
)} {step === "otp" && (
setOtpCode(e.target.value)} maxLength={6} disabled={loading} className="h-11 text-center tracking-widest text-lg" />
)}
{t.loginTerms?.prefix} {t.loginTerms?.link} {t.loginTerms?.suffix}
) }