From 57a83cd393a95d636d281d6af2bb02ffe57172a8 Mon Sep 17 00:00:00 2001 From: Nurfog Date: Wed, 9 Sep 2026 16:20:24 -0300 Subject: [PATCH] feat: login con idUsuario --- src/app/api/auth/route.ts | 27 +++++++++++++-------------- src/app/login/page.tsx | 2 +- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/app/api/auth/route.ts b/src/app/api/auth/route.ts index 06b0b15..ef19e21 100644 --- a/src/app/api/auth/route.ts +++ b/src/app/api/auth/route.ts @@ -4,32 +4,31 @@ import { COOKIE, isAllowed, signSession, SESSION_MAX_AGE } from "@/lib/auth"; const API_URL = process.env.NEXT_PUBLIC_API_URL || "https://api.devops.norteamericano.cl"; -// Login IT: valida en el backend (sige_sam_v3.usuario, plano o SHA256) -// y exige que el correo esté en IT_USERS (allowlist). +// Login IT: USUARIO = idUsuario. Valida en el backend +// (sige_sam_v3.usuario, plano o SHA256) y exige que el email +// retornado esté en IT_USERS (allowlist). export async function POST(req: NextRequest) { try { - const { email, password } = await req.json(); - const mail = String(email || "").trim().toLowerCase(); - if (!mail || !password) { + const { idUsuario, email, password } = await req.json(); + const id = String(idUsuario ?? email ?? "").trim(); + if (!id || !password) { return NextResponse.json({ error: "Faltan credenciales" }, { status: 400 }); } - if (!isAllowed(mail)) { - await new Promise((r) => setTimeout(r, 600)); - return NextResponse.json({ error: "Credenciales inválidas" }, { status: 401 }); - } - let ok = false; + let mail = ""; try { const res = await fetch(`${API_URL}/api/Auth/it-login`, { method: "POST", headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ email: mail, password: String(password) }), + body: JSON.stringify({ idUsuario: id, email: id, password: String(password) }), cache: "no-store", }); - ok = res.ok; + if (!res.ok) throw new Error("backend"); + const j = await res.json(); + mail = String(j?.user?.email || "").trim().toLowerCase(); } catch { - ok = false; + mail = ""; } - if (!ok) { + if (!mail || !isAllowed(mail)) { await new Promise((r) => setTimeout(r, 600)); return NextResponse.json({ error: "Credenciales inválidas" }, { status: 401 }); } diff --git a/src/app/login/page.tsx b/src/app/login/page.tsx index cf024b6..4e65b95 100644 --- a/src/app/login/page.tsx +++ b/src/app/login/page.tsx @@ -19,7 +19,7 @@ function LoginForm() { const res = await fetch("/api/auth", { method: "POST", headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ email: rut, password: clave }), + body: JSON.stringify({ idUsuario: rut, password: clave }), }); const j = await res.json(); if (!res.ok) throw new Error(j.error || `HTTP ${res.status}`);