49 lines
1.8 KiB
JavaScript
49 lines
1.8 KiB
JavaScript
"use client";
|
|
import { useState } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
import Link from "next/link";
|
|
|
|
export default function LoginPage() {
|
|
const [email, setEmail] = useState("");
|
|
const [password, setPassword] = useState("");
|
|
const [error, setError] = useState("");
|
|
const [loading, setLoading] = useState(false);
|
|
const router = useRouter();
|
|
|
|
async function onSubmit(e) {
|
|
e.preventDefault();
|
|
setError(""); setLoading(true);
|
|
try {
|
|
const r = await fetch("/api/login", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ email, password }),
|
|
});
|
|
const data = await r.json();
|
|
if (!r.ok) { setError(data.error || "Error al iniciar sesión"); return; }
|
|
router.push("/");
|
|
router.refresh();
|
|
} catch { setError("Error de conexión"); }
|
|
finally { setLoading(false); }
|
|
}
|
|
|
|
return (
|
|
<div className="auth">
|
|
<div className="auth-card">
|
|
<h1>Iniciar sesión</h1>
|
|
<p className="auth-sub">Bienvenido de nuevo a Nova Store.</p>
|
|
<form onSubmit={onSubmit} className="auth-form">
|
|
<label>Email
|
|
<input type="email" value={email} onChange={(e) => setEmail(e.target.value)} placeholder="tu@email.com" required />
|
|
</label>
|
|
<label>Contraseña
|
|
<input type="password" value={password} onChange={(e) => setPassword(e.target.value)} placeholder="••••••••" required />
|
|
</label>
|
|
{error && <p className="auth-error">{error}</p>}
|
|
<button className="btn btn-primary" disabled={loading}>{loading ? "Entrando…" : "Entrar"}</button>
|
|
</form>
|
|
<p className="auth-alt">¿No tenés cuenta? <Link href="/register">Registrate</Link></p>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|