import { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { Lock, Mail, Phone, ChevronLeft, KeyRound } from 'lucide-react'; import { toast } from 'react-hot-toast'; import api from '../services/api'; import { useAdminAuthStore } from '../store/adminAuthStore'; import type { ApiResponse, AuthResponseData } from '../types/admin'; export default function Login() { const [loginMode, setLoginMode] = useState<'password' | 'otp'>('password'); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [phoneNumber, setPhoneNumber] = useState(''); const [otpCode, setOtpCode] = useState(''); const [otpSent, setOtpSent] = useState(false); const [isLoading, setIsLoading] = useState(false); const navigate = useNavigate(); const setAuth = useAdminAuthStore((state) => state.setAuth); const handlePasswordLogin = async (e: React.FormEvent) => { e.preventDefault(); setIsLoading(true); try { const response = await api.post>('/auth/admin-login', { email, password }); if (response.data?.success && response.data.data) { setAuth(response.data.data.accessToken, response.data.data.user); toast.success('ورود با موفقیت انجام شد'); navigate('/'); } else { toast.error('اطلاعات ورود نامعتبر است'); } } catch { // Toast notification is handled by api.ts interceptor } finally { setIsLoading(false); } }; const handleSendOtp = async (e: React.FormEvent) => { e.preventDefault(); if (!phoneNumber || phoneNumber.length < 10) { toast.error('شماره موبایل معتبر وارد کنید'); return; } setIsLoading(true); try { const response = await api.post('/auth/send-otp', { phoneNumber }); if (response.data?.success) { toast.success(response.data.message || 'کد تایید پیامک شد'); setOtpSent(true); } } catch { toast.error('خطا در ارسال کد تایید'); } finally { setIsLoading(false); } }; const handleVerifyOtp = async (e: React.FormEvent) => { e.preventDefault(); if (!otpCode || otpCode.length < 4) { toast.error('کد تایید معتبر وارد کنید'); return; } setIsLoading(true); try { const response = await api.post>('/auth/verify-otp', { phoneNumber, code: otpCode }); if (response.data?.success && response.data.data) { setAuth(response.data.data.accessToken, response.data.data.user); toast.success('ورود موفقیت‌آمیز بود'); navigate('/'); } else { toast.error('کد تایید نامعتبر است'); } } catch { toast.error('کد تایید اشتباه یا منقضی شده است'); } finally { setIsLoading(false); } }; return (

ورود به پنل مدیریت

کانینا | لابراتوار تخصصی مکمل‌های درمانی حیوانات

{/* Toggle Login Mode */}
{loginMode === 'password' ? (
setEmail(e.target.value)} className="block w-full pl-3 pr-10 py-3 border border-gray-200 rounded-xl focus:ring-purple-500 focus:border-purple-500 sm:text-sm bg-gray-50 font-bold transition-all" placeholder="admin@canino-iran.com" dir="ltr" />
setPassword(e.target.value)} className="block w-full pl-3 pr-10 py-3 border border-gray-200 rounded-xl focus:ring-purple-500 focus:border-purple-500 sm:text-sm bg-gray-50 font-bold transition-all text-left" placeholder="••••••••" dir="ltr" />
) : (
{!otpSent ? (
setPhoneNumber(e.target.value)} className="block w-full pl-3 pr-10 py-3 border border-gray-200 rounded-xl focus:ring-purple-500 focus:border-purple-500 sm:text-sm bg-gray-50 font-bold transition-all text-left" placeholder="09123456789" dir="ltr" />
) : (
setOtpCode(e.target.value)} className="block w-full pl-3 pr-10 py-3 border border-gray-200 rounded-xl focus:ring-purple-500 focus:border-purple-500 sm:text-sm bg-gray-50 font-bold transition-all text-center tracking-widest text-lg" placeholder="12345" dir="ltr" />
)}
)}
); }