refactor(proxy): simplify isInitialized function and remove cookie caching

This commit is contained in:
2026-03-12 10:18:06 +08:00
parent 4703ef3548
commit e759576b58

View File

@@ -3,32 +3,20 @@ import { NextRequest, NextResponse } from "next/server";
const CSMS_INTERNAL_URL = const CSMS_INTERNAL_URL =
process.env.CSMS_INTERNAL_URL ?? process.env.NEXT_PUBLIC_CSMS_URL ?? "http://localhost:3001"; process.env.CSMS_INTERNAL_URL ?? process.env.NEXT_PUBLIC_CSMS_URL ?? "http://localhost:3001";
/** 检查 CSMS 是否已完成初始化(有用户存在)。使用 cookie 缓存结果,避免每次请求都查询。 */ /** 检查 CSMS 是否已完成初始化(有用户存在)。 */
async function isInitialized( async function isInitialized(request: NextRequest): Promise<boolean> {
request: NextRequest,
useCache = true,
): Promise<{ initialized: boolean; fromCache: boolean }> {
// 读缓存 cookie仅在 useCache=true 时使用,避免 DB 重置后缓存陈旧)
if (useCache) {
const cached = request.cookies.get("helios_setup_done");
if (cached?.value === "1") {
return { initialized: true, fromCache: true };
}
}
try { try {
const res = await fetch(`${CSMS_INTERNAL_URL}/api/setup`, { const res = await fetch(`${CSMS_INTERNAL_URL}/api/setup`, {
method: "GET", method: "GET",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
// 短超时,避免阻塞
signal: AbortSignal.timeout(3000), signal: AbortSignal.timeout(3000),
}); });
if (!res.ok) return { initialized: true, fromCache: false }; // 出错时放行,不阻止访问 if (!res.ok) return true; // 出错时放行,不阻止访问
const data = (await res.json()) as { initialized: boolean }; const data = (await res.json()) as { initialized: boolean };
return { initialized: data.initialized, fromCache: false }; return data.initialized;
} catch { } catch {
// 无法连接 CSMS 时放行,不强制跳转 // 无法连接 CSMS 时放行,不强制跳转
return { initialized: true, fromCache: false }; return true;
} }
} }
@@ -37,27 +25,16 @@ export async function proxy(request: NextRequest) {
// /setup 页面:已初始化则跳转登录 // /setup 页面:已初始化则跳转登录
if (pathname === "/setup") { if (pathname === "/setup") {
const { initialized, fromCache } = await isInitialized(request); if (await isInitialized(request)) {
if (initialized) {
return NextResponse.redirect(new URL("/login", request.url)); return NextResponse.redirect(new URL("/login", request.url));
} }
const res = NextResponse.next(); return NextResponse.next();
if (!fromCache) {
// 未初始化,确保缓存 cookie 不存在(若之前意外设置了)
res.cookies.delete("helios_setup_done");
}
return res;
} }
// /dashboard 路由:检查 session未登录跳转 /login // /dashboard 路由:检查 session未登录跳转 /login
if (pathname.startsWith("/dashboard")) { if (pathname.startsWith("/dashboard")) {
const { initialized, fromCache } = await isInitialized(request); if (!(await isInitialized(request))) {
return NextResponse.redirect(new URL("/setup", request.url));
// 未初始化,先去 setup
if (!initialized) {
const res = NextResponse.redirect(new URL("/setup", request.url));
if (!fromCache) res.cookies.delete("helios_setup_done");
return res;
} }
const sessionCookie = const sessionCookie =
@@ -68,30 +45,18 @@ export async function proxy(request: NextRequest) {
const loginUrl = new URL("/login", request.url); const loginUrl = new URL("/login", request.url);
const fromPath = request.nextUrl.search ? pathname + request.nextUrl.search : pathname; const fromPath = request.nextUrl.search ? pathname + request.nextUrl.search : pathname;
loginUrl.searchParams.set("from", fromPath); loginUrl.searchParams.set("from", fromPath);
const res = NextResponse.redirect(loginUrl); return NextResponse.redirect(loginUrl);
if (!fromCache)
res.cookies.set("helios_setup_done", "1", { path: "/", httpOnly: true, sameSite: "lax" });
return res;
} }
const res = NextResponse.next(); return NextResponse.next();
if (!fromCache)
res.cookies.set("helios_setup_done", "1", { path: "/", httpOnly: true, sameSite: "lax" });
return res;
} }
// /login 路由:未初始化则跳转 /setup(不使用缓存,防止 DB 重置后缓存陈旧) // /login 路由:未初始化则跳转 /setup
if (pathname === "/login") { if (pathname === "/login") {
const { initialized, fromCache } = await isInitialized(request, false); if (!(await isInitialized(request))) {
if (!initialized) { return NextResponse.redirect(new URL("/setup", request.url));
const res = NextResponse.redirect(new URL("/setup", request.url));
if (!fromCache) res.cookies.delete("helios_setup_done");
return res;
} }
const res = NextResponse.next(); return NextResponse.next();
if (!fromCache)
res.cookies.set("helios_setup_done", "1", { path: "/", httpOnly: true, sameSite: "lax" });
return res;
} }
return NextResponse.next(); return NextResponse.next();