refactor(middleware): improve isInitialized function and cookie handling
This commit is contained in:
@@ -1,16 +1,19 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
|
||||||
const CSMS_INTERNAL_URL =
|
const CSMS_INTERNAL_URL =
|
||||||
process.env.CSMS_INTERNAL_URL ??
|
process.env.CSMS_INTERNAL_URL ?? process.env.NEXT_PUBLIC_CSMS_URL ?? "http://localhost:3001";
|
||||||
process.env.NEXT_PUBLIC_CSMS_URL ??
|
|
||||||
"http://localhost:3001";
|
|
||||||
|
|
||||||
/** 检查 CSMS 是否已完成初始化(有用户存在)。使用 cookie 缓存结果,避免每次请求都查询。 */
|
/** 检查 CSMS 是否已完成初始化(有用户存在)。使用 cookie 缓存结果,避免每次请求都查询。 */
|
||||||
async function isInitialized(request: NextRequest): Promise<{ initialized: boolean; fromCache: boolean }> {
|
async function isInitialized(
|
||||||
// 读缓存 cookie
|
request: NextRequest,
|
||||||
const cached = request.cookies.get("helios_setup_done");
|
useCache = true,
|
||||||
if (cached?.value === "1") {
|
): Promise<{ initialized: boolean; fromCache: boolean }> {
|
||||||
return { initialized: true, fromCache: true };
|
// 读缓存 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 {
|
||||||
@@ -66,25 +69,28 @@ export async function middleware(request: NextRequest) {
|
|||||||
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);
|
const res = NextResponse.redirect(loginUrl);
|
||||||
if (!fromCache) res.cookies.set("helios_setup_done", "1", { path: "/", httpOnly: true, sameSite: "lax" });
|
if (!fromCache)
|
||||||
|
res.cookies.set("helios_setup_done", "1", { path: "/", httpOnly: true, sameSite: "lax" });
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = NextResponse.next();
|
const res = NextResponse.next();
|
||||||
if (!fromCache) res.cookies.set("helios_setup_done", "1", { path: "/", httpOnly: true, sameSite: "lax" });
|
if (!fromCache)
|
||||||
|
res.cookies.set("helios_setup_done", "1", { path: "/", httpOnly: true, sameSite: "lax" });
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
// /login 路由:未初始化则跳转 /setup
|
// /login 路由:未初始化则跳转 /setup(不使用缓存,防止 DB 重置后缓存陈旧)
|
||||||
if (pathname === "/login") {
|
if (pathname === "/login") {
|
||||||
const { initialized, fromCache } = await isInitialized(request);
|
const { initialized, fromCache } = await isInitialized(request, false);
|
||||||
if (!initialized) {
|
if (!initialized) {
|
||||||
const res = NextResponse.redirect(new URL("/setup", request.url));
|
const res = NextResponse.redirect(new URL("/setup", request.url));
|
||||||
if (!fromCache) res.cookies.delete("helios_setup_done");
|
if (!fromCache) res.cookies.delete("helios_setup_done");
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
const res = NextResponse.next();
|
const res = NextResponse.next();
|
||||||
if (!fromCache) res.cookies.set("helios_setup_done", "1", { path: "/", httpOnly: true, sameSite: "lax" });
|
if (!fromCache)
|
||||||
|
res.cookies.set("helios_setup_done", "1", { path: "/", httpOnly: true, sameSite: "lax" });
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user