function Login({ onLogin }) { const [username, setUsername] = React.useState(""); const [password, setPassword] = React.useState(""); const [error, setError] = React.useState(null); const [loading, setLoading] = React.useState(false); async function handleSubmit(e) { e.preventDefault(); setError(null); setLoading(true); try { const resp = await fetch("/auth/login", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ username, password }), }); if (resp.ok) { const user = await resp.json(); onLogin(user); } else { setError("Nome utente o password non validi."); } } catch { setError("Errore di rete. Riprova."); } finally { setLoading(false); } } return (
Spesa
setUsername(e.target.value)} className="border border-gray-300 rounded-lg px-3 py-2 focus:outline-none focus:ring-2 focus:ring-[#2d7a3a] focus:border-transparent" />
setPassword(e.target.value)} className="border border-gray-300 rounded-lg px-3 py-2 focus:outline-none focus:ring-2 focus:ring-[#2d7a3a] focus:border-transparent" />
{error &&

{error}

}
); }