'use client' import { useState } from 'react' import { useRouter } from 'next/navigation' import { Button } from '@/components/ui/button' import { apiFetch } from '@/lib/csrf' export default function LoginPage() { const router = useRouter() const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [error, setError] = useState('') const [loading, setLoading] = useState(false) async function handleSubmit(e: React.FormEvent) { e.preventDefault() setError('') setLoading(true) try { const res = await apiFetch('/api/auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password }), }) if (res.ok) { router.push('/') router.refresh() } else { const data = await res.json() setError(data.error ?? 'Login failed') } } catch { setError('Network error, please try again') } finally { setLoading(false) } } return (