import { useState } from 'react'; import { useNavigate, Link } from 'react-router-dom'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { z } from 'zod'; import { apiClient } from '@/lib/api-client'; import { useAuthStore } from '@/store/auth-store'; import { Button } from '@/components/ui/button'; import { Loader2 } from 'lucide-react'; const loginSchema = z.object({ email: z.string().email('Invalid email address'), password: z.string().min(8, 'Password must be at least 8 characters'), }); type LoginForm = z.infer; export function LoginPage() { const navigate = useNavigate(); const { setAuth } = useAuthStore(); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const { register, handleSubmit, formState: { errors }, } = useForm({ resolver: zodResolver(loginSchema), }); const onSubmit = async (data: LoginForm) => { setIsLoading(true); setError(null); try { const response = await apiClient.login(data.email, data.password); setAuth(response.user, response.token); navigate('/dashboard'); } catch (err: any) { setError(err.response?.data?.error || 'Login failed. Please try again.'); } finally { setIsLoading(false); } }; return (
{errors.email && (

{errors.email.message}

)}
{errors.password && (

{errors.password.message}

)}
{error && (

{error}

)}
Don't have an account?{' '} Sign up
); }