/** 私有页面会话路由,默认登录后进入数字人空间。 */ import { NextRequest, NextResponse } from "next/server"; import { digitalLoginPath, safeDigitalReturnTo } from "@/lib/digital-routes"; const protectedRoutes = [ "/discover", "/chats", "/characters", "/library", "/profile", "/dashboard", "/tasks", "/schedule", "/announcements", "/store", "/me", ]; const publicRoutes = ["/", "/login"]; const cookieName = process.env.ROLE_USER_SESSION_COOKIE || "role_user_session"; /** 路由级登录保护;深链携带站内回跳目标,不丢失具体聊天地址。 */ export function proxy(request: NextRequest) { const path = request.nextUrl.pathname; const hasSession = Boolean(request.cookies.get(cookieName)?.value); const isProtected = protectedRoutes.some( (route) => path === route || path.startsWith(`${route}/`), ); const isPublic = publicRoutes.includes(path); if (isProtected && !hasSession) { return NextResponse.redirect( new URL(digitalLoginPath(path + request.nextUrl.search), request.url), ); } if (isPublic && hasSession) { return NextResponse.redirect( new URL( path === "/login" ? safeDigitalReturnTo(request.nextUrl.searchParams.get("next")) : "/discover", request.url, ), ); } return NextResponse.next(); } /** 只拦截业务页面,不影响 BFF、静态资源和安装文件。 */ export const config = { matcher: [ "/((?!api|_next/static|_next/image|manifest.webmanifest|sw.js|.*\\..*).*)", ], };