nova-store/app/api/login/route.js

22 lines
888 B
JavaScript
Raw Normal View History

import { NextResponse } from "next/server";
import db from "../../lib/db";
import { verifyPassword, signSession } from "../../lib/auth";
export async function POST(req) {
let body;
try { body = await req.json(); } catch { return NextResponse.json({ error: "Solicitud inválida" }, { status: 400 }); }
const email = (body.email || "").trim().toLowerCase();
const password = body.password || "";
const user = db.prepare("SELECT * FROM users WHERE email = ?").get(email);
if (!user || !verifyPassword(password, user.password))
return NextResponse.json({ error: "Email o contraseña incorrectos" }, { status: 401 });
const res = NextResponse.json({ ok: true, user: { id: user.id, name: user.name, email: user.email } });
res.cookies.set("session", signSession(user.id), {
httpOnly: true, sameSite: "lax", path: "/", maxAge: 60 * 60 * 24 * 7,
});
return res;
}