feat: login con idUsuario
This commit is contained in:
1 parent
d1d8bf03ae
commit
57a83cd393
2 files changed
+14
-15
No files matched your search
+13
-14
@@ -4,32 +4,31 @@ import { COOKIE, isAllowed, signSession, SESSION_MAX_AGE } from "@/lib/auth";
|
|||||||
const API_URL =
|
const API_URL =
|
||||||
process.env.NEXT_PUBLIC_API_URL || "https://api.devops.norteamericano.cl";
|
process.env.NEXT_PUBLIC_API_URL || "https://api.devops.norteamericano.cl";
|
||||||
|
|
||||||
// Login IT: valida en el backend (sige_sam_v3.usuario, plano o SHA256)
|
// Login IT: USUARIO = idUsuario. Valida en el backend
|
||||||
// y exige que el correo esté en IT_USERS (allowlist).
|
// (sige_sam_v3.usuario, plano o SHA256) y exige que el email
|
||||||
|
// retornado esté en IT_USERS (allowlist).
|
||||||
export async function POST(req: NextRequest) {
|
export async function POST(req: NextRequest) {
|
||||||
try {
|
try {
|
||||||
const { email, password } = await req.json();
|
const { idUsuario, email, password } = await req.json();
|
||||||
const mail = String(email || "").trim().toLowerCase();
|
const id = String(idUsuario ?? email ?? "").trim();
|
||||||
if (!mail || !password) {
|
if (!id || !password) {
|
||||||
return NextResponse.json({ error: "Faltan credenciales" }, { status: 400 });
|
return NextResponse.json({ error: "Faltan credenciales" }, { status: 400 });
|
||||||
}
|
}
|
||||||
if (!isAllowed(mail)) {
|
let mail = "";
|
||||||
await new Promise((r) => setTimeout(r, 600));
|
|
||||||
return NextResponse.json({ error: "Credenciales inválidas" }, { status: 401 });
|
|
||||||
}
|
|
||||||
let ok = false;
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`${API_URL}/api/Auth/it-login`, {
|
const res = await fetch(`${API_URL}/api/Auth/it-login`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
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",
|
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 {
|
} catch {
|
||||||
ok = false;
|
mail = "";
|
||||||
}
|
}
|
||||||
if (!ok) {
|
if (!mail || !isAllowed(mail)) {
|
||||||
await new Promise((r) => setTimeout(r, 600));
|
await new Promise((r) => setTimeout(r, 600));
|
||||||
return NextResponse.json({ error: "Credenciales inválidas" }, { status: 401 });
|
return NextResponse.json({ error: "Credenciales inválidas" }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ function LoginForm() {
|
|||||||
const res = await fetch("/api/auth", {
|
const res = await fetch("/api/auth", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({ email: rut, password: clave }),
|
body: JSON.stringify({ idUsuario: rut, password: clave }),
|
||||||
});
|
});
|
||||||
const j = await res.json();
|
const j = await res.json();
|
||||||
if (!res.ok) throw new Error(j.error || `HTTP ${res.status}`);
|
if (!res.ok) throw new Error(j.error || `HTTP ${res.status}`);
|
||||||
|
|||||||
Reference in new issue
Block a user