// Auteur : Simon-Pierre Boucher — contact@spboucher.ai import { NextRequest, NextResponse } from "next/server"; import { findUserByEmail, touchLastLogin } from "@/lib/db"; import { verifyPassword, mintSessionToken, SESSION_COOKIE, sessionCookieOptions, } from "@/lib/auth"; export async function POST(req: NextRequest) { let body: { email?: string; password?: string }; try { body = await req.json(); } catch { return NextResponse.json({ error: "Requête invalide." }, { status: 400 }); } const email = (body.email ?? "").trim().toLowerCase(); const password = body.password ?? ""; const user = findUserByEmail(email); if (!user || !user.password_hash) { return NextResponse.json( { error: user ? "Ce KA ID a été créé avec Google — utilisez « Continuer avec Google »." : "Courriel ou mot de passe incorrect.", }, { status: 401 }, ); } if (!verifyPassword(password, user.password_hash)) return NextResponse.json( { error: "Courriel ou mot de passe incorrect." }, { status: 401 }, ); touchLastLogin(user.id); const res = NextResponse.json({ ok: true }); res.cookies.set( SESSION_COOKIE, await mintSessionToken(user.id), sessionCookieOptions, ); return res; }