diff --git a/src/pages/Login.tsx b/src/pages/Login.tsx index c14dbfc..82a8c3f 100644 --- a/src/pages/Login.tsx +++ b/src/pages/Login.tsx @@ -1,19 +1,22 @@ import { useState } from 'react'; -import { Navigate } from 'react-router-dom'; -import { Eye, EyeOff, Lock, Mail } from 'lucide-react'; +import { Navigate, useNavigate } from 'react-router-dom'; +import { Eye, EyeOff, Lock, User } from 'lucide-react'; import { useForm } from 'react-hook-form'; import { yupResolver } from '@hookform/resolvers/yup'; import { useAuth } from '../contexts/AuthContext'; import { Button } from '../components/ui/Button'; import { Input } from '../components/ui/Input'; import { loginSchema, LoginFormData } from '../utils/validationSchemas'; +import { useLogin } from './auth/core/_hooks'; export const Login = () => { - const { isAuthenticated, login } = useAuth(); + const { isAuthenticated, restoreSession } = useAuth(); + const navigate = useNavigate(); const [showPassword, setShowPassword] = useState(false); - const [loading, setLoading] = useState(false); const [error, setError] = useState(''); + const { mutate: login, isPending: isLoading } = useLogin(); + const { register, handleSubmit, @@ -28,19 +31,17 @@ export const Login = () => { } const onSubmit = async (data: LoginFormData) => { - setLoading(true); setError(''); - try { - const success = await login(data.email, data.password); - if (!success) { - setError('ایمیل یا رمز عبور اشتباه است'); + login(data, { + onSuccess: () => { + restoreSession(); + navigate('/'); + }, + onError: () => { + setError('نام کاربری یا رمز عبور اشتباه است'); } - } catch (error) { - setError('خطایی رخ داده است. لطفا دوباره تلاش کنید'); - } finally { - setLoading(false); - } + }); }; return ( @@ -61,12 +62,12 @@ export const Login = () => {
@@ -79,7 +80,7 @@ export const Login = () => {
{
)} -
-

اطلاعات تست:

-

ایمیل: admin@test.com

-

رمز عبور: admin123

-
-