import { useState } from 'react'; import { useNavigate, useLocation } from 'react-router-dom'; import { api } from '../api'; export default function Login({ onLogin }) { const [password, setPassword] = useState(''); const [error, setError] = useState(''); const [loading, setLoading] = useState(false); const navigate = useNavigate(); const location = useLocation(); const from = location.state?.from?.pathname || location.pathname || '/'; async function handleSubmit(e) { e.preventDefault(); setError(''); setLoading(true); try { const res = await api('/auth/login', { method: 'POST', body: { password } }); if (!res.ok) { setError('Invalid password'); setLoading(false); return; } onLogin?.(); navigate(from, { replace: true }); } catch { setError('Something went wrong'); } setLoading(false); } return (