import { NextResponse, type NextRequest } from "next/server"; import { getToken } from "@/app/lib/server/session"; import { maybeRefreshSession } from "@/app/lib/server/sessionRefresh"; import { callMobileApi } from "@/app/lib/server/mobileApi"; import { errorResponse, unauthorized } from "@/app/lib/server/apiRoute"; import type { AdminMeResponse } from "@/app/lib/types"; // GET → /api/admin/me. The httpOnly colite_admin cookie carries only the token // (and resetPasswordRequired); roles/aclMenu are hydrated here on every call // rather than persisted in the cookie (spec: "Session Hydration Loads Roles // And ACL Menu", "The ACL tree is not stored in the cookie"). // // Sliding session: an active admin's token is refreshed near expiry (up to the // absolute cap). This handler is fetch-driven (never a prefetch target), // but per repo policy the cookie rewrite stays off a prefetch GET via the // Next-Router-Prefetch guard. export async function GET(req: NextRequest) { if (req.headers.get("next-router-prefetch") !== "1") { await maybeRefreshSession(); } const token = await getToken(); if (!token) return unauthorized(); const result = await callMobileApi("/api/admin/me", { token }); if (!result.ok) return errorResponse(result); const { id, username, name, roles, aclMenu, branch } = result.data; return NextResponse.json({ id, username, name, roles, aclMenu, branch: branch ?? null }); }