mobile-api now records the browser user agent the portal forwards as X-Client-User-Agent instead of the portal server's own. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
47 lines
2.2 KiB
TypeScript
47 lines
2.2 KiB
TypeScript
import { NextResponse, type NextRequest } from "next/server";
|
|
import { callMobileApi } from "@/app/lib/server/mobileApi";
|
|
import { setSession } from "@/app/lib/server/session";
|
|
import { clientContextHeaders } from "@/app/lib/server/clientContext";
|
|
import { errorResponse, unauthorized } from "@/app/lib/server/apiRoute";
|
|
import { clearMarkerOn } from "@/app/lib/server/signedInElsewhere";
|
|
import type { AdminLoginResponse } from "@/app/lib/types";
|
|
|
|
// POST → /api/admin/auth/login.
|
|
//
|
|
// The JWT is set into the httpOnly colite_admin cookie and never returned in the
|
|
// response body (spec: "The Admin JWT Lives Only In An httpOnly Cookie").
|
|
// roles/aclMenu are re-fetched via /api/auth/session hydration rather than
|
|
// echoed back here, keeping this handler's response minimal.
|
|
export async function POST(req: NextRequest) {
|
|
const body = await req.json().catch(() => ({}));
|
|
const { username, password } = body as { username?: string; password?: string };
|
|
|
|
const identifier = (username ?? "").trim();
|
|
if (!identifier || !password) {
|
|
return NextResponse.json({ error: "Username and password are required." }, { status: 400 });
|
|
}
|
|
|
|
const result = await callMobileApi<AdminLoginResponse>("/api/admin/auth/login", {
|
|
method: "POST",
|
|
body: { username: identifier, password },
|
|
// Real-client context for the login_audit trail (X-Client-Ip / X-Client-User-Agent).
|
|
headers: clientContextHeaders(req),
|
|
});
|
|
|
|
if (!result.ok) {
|
|
// The mobile-api answers bad credentials with HTTP 401 directly (not the
|
|
// expired-token signal), so surface it as 401 rather than the generic 400.
|
|
// A locked account is also a 401, but carries code ACCOUNT_LOCKED, which the
|
|
// login page needs to show its own message; errorResponse keeps the code.
|
|
if (result.status === 401 && result.code !== "ACCOUNT_LOCKED") return unauthorized(result.error);
|
|
return errorResponse(result);
|
|
}
|
|
|
|
const { token, name, resetPasswordRequired } = result.data;
|
|
const resetRequired = resetPasswordRequired === "true";
|
|
await setSession(token, resetRequired);
|
|
|
|
// one-session-per-account: signed in again, so the login page stops saying this browser was signed out.
|
|
return clearMarkerOn(NextResponse.json({ name, resetPasswordRequired: resetRequired }));
|
|
}
|