Files
co-lite/solvway-admin-portal/app/api/auth/login/route.ts
T
ngqileandClaude Opus 5.5 6598b58cb8 feat: locked sign-in message and Change password dialog in the portal
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
2026-11-30 14:16:11 +08:00

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 }));
}