chore(router): disable router proxy and re-exports by commenting out
Comment out `src/router/index.ts` and `src/router/proxy.ts` to temporarily disable the Next.js 16 proxy (auth-driven redirects) and router re-exports. This effectively turns off the centralized route guard logic and stops the proxy from intercepting requests until further iteration.
This commit is contained in:
+14
-14
@@ -1,14 +1,14 @@
|
|||||||
/**
|
// /**
|
||||||
* Router 公共导出
|
// * Router 公共导出
|
||||||
*
|
// *
|
||||||
* 集中管理:
|
// * 集中管理:
|
||||||
* - 路由常量(ROUTES、ROUTE_BUILDERS、AUTH_ONLY_ROUTES、PUBLIC_ROUTES、ALL_STATIC_ROUTES)
|
// * - 路由常量(ROUTES、ROUTE_BUILDERS、AUTH_ONLY_ROUTES、PUBLIC_ROUTES、ALL_STATIC_ROUTES)
|
||||||
* - Next.js 16 Proxy(CDN 端鉴权重定向)
|
// * - Next.js 16 Proxy(CDN 端鉴权重定向)
|
||||||
*
|
// *
|
||||||
* 业务层导入示例:
|
// * 业务层导入示例:
|
||||||
* ```ts
|
// * ```ts
|
||||||
* import { ROUTES, AUTH_ONLY_ROUTES, type Route } from "@/router";
|
// * import { ROUTES, AUTH_ONLY_ROUTES, type Route } from "@/router";
|
||||||
* ```
|
// * ```
|
||||||
*/
|
// */
|
||||||
export * from "./routes";
|
// export * from "./routes";
|
||||||
export { proxy, config as proxyConfig } from "./proxy";
|
// export { proxy, config as proxyConfig } from "./proxy";
|
||||||
|
|||||||
+88
-88
@@ -1,101 +1,101 @@
|
|||||||
/**
|
// /**
|
||||||
* Next.js 16 Proxy(原 `middleware.ts`)
|
// * Next.js 16 Proxy(原 `middleware.ts`)
|
||||||
*
|
// *
|
||||||
* 行为(鉴权路由统一在此处理,业务层组件不再做 auth-driven 跳转):
|
// * 行为(鉴权路由统一在此处理,业务层组件不再做 auth-driven 跳转):
|
||||||
* - 已登录(NextAuth `next-auth.session-token` cookie 存在)访问 `/splash` 或 `/auth`
|
// * - 已登录(NextAuth `next-auth.session-token` cookie 存在)访问 `/splash` 或 `/auth`
|
||||||
* → 308 重定向到 `/chat`
|
// * → 308 重定向到 `/chat`
|
||||||
* - 未登录(cookie 缺失)访问 `/chat` 或 `/sidebar`
|
// * - 未登录(cookie 缺失)访问 `/chat` 或 `/sidebar`
|
||||||
* → 308 重定向到 `/splash`(首界面)
|
// * → 308 重定向到 `/splash`(首界面)
|
||||||
* - 其他情况透传(`NextResponse.next()`)
|
// * - 其他情况透传(`NextResponse.next()`)
|
||||||
*
|
// *
|
||||||
* 重要约束:
|
// * 重要约束:
|
||||||
* - **不**做自定义 cookie 持久化:统一用 NextAuth 内置 `next-auth.session-token` /
|
// * - **不**做自定义 cookie 持久化:统一用 NextAuth 内置 `next-auth.session-token` /
|
||||||
* `__Secure-next-auth.session-token`(v5 默认 httpOnly + secure)作信号。
|
// * `__Secure-next-auth.session-token`(v5 默认 httpOnly + secure)作信号。
|
||||||
* - 真正的鉴权门在 Client 侧 `useSession()`(next-auth/react),用于 UI 状态。
|
// * - 真正的鉴权门在 Client 侧 `useSession()`(next-auth/react),用于 UI 状态。
|
||||||
* - 路由跳转(**唯一**鉴权跳转点)由本 proxy 集中处理。
|
// * - 路由跳转(**唯一**鉴权跳转点)由本 proxy 集中处理。
|
||||||
* - 副作用:邮箱登录用户(不走 NextAuth)不会被本 proxy 识别为已登录。
|
// * - 副作用:邮箱登录用户(不走 NextAuth)不会被本 proxy 识别为已登录。
|
||||||
* 邮箱登录在后续轮次若要恢复 proxy 行为,需引入 NextAuth EmailProvider 或独立
|
// * 邮箱登录在后续轮次若要恢复 proxy 行为,需引入 NextAuth EmailProvider 或独立
|
||||||
* email-session 体系。
|
// * email-session 体系。
|
||||||
*
|
// *
|
||||||
* Next 16 重要变更(参见 `node_modules/next/dist/docs/01-app/03-api-reference/03-file-conventions/proxy.md`):
|
// * Next 16 重要变更(参见 `node_modules/next/dist/docs/01-app/03-api-reference/03-file-conventions/proxy.md`):
|
||||||
* - 文件从 `middleware.ts` 重命名为 `proxy.ts`;`middleware` 仍可用但已 deprecated。
|
// * - 文件从 `middleware.ts` 重命名为 `proxy.ts`;`middleware` 仍可用但已 deprecated。
|
||||||
* - 默认 Node.js runtime;**不**支持 `runtime` 配置(设置会抛错)。
|
// * - 默认 Node.js runtime;**不**支持 `runtime` 配置(设置会抛错)。
|
||||||
* - 函数导出名建议为 `proxy`(可默认导出)。
|
// * - 函数导出名建议为 `proxy`(可默认导出)。
|
||||||
*
|
// *
|
||||||
* 用 `src/` 时 proxy 必须放在 `src/` 内(参见 `src-folder.md`)。
|
// * 用 `src/` 时 proxy 必须放在 `src/` 内(参见 `src-folder.md`)。
|
||||||
*/
|
// */
|
||||||
|
|
||||||
import { NextResponse } from "next/server";
|
// import { NextResponse } from "next/server";
|
||||||
import type { NextRequest } from "next/server";
|
// import type { NextRequest } from "next/server";
|
||||||
|
|
||||||
import { Logger } from "@/utils/logger";
|
// import { Logger } from "@/utils/logger";
|
||||||
|
|
||||||
import {
|
// import {
|
||||||
AUTH_ONLY_ROUTES,
|
// AUTH_ONLY_ROUTES,
|
||||||
PROTECTED_ROUTES,
|
// PROTECTED_ROUTES,
|
||||||
ROUTES,
|
// ROUTES,
|
||||||
} from "@/router/routes";
|
// } from "@/router/routes";
|
||||||
|
|
||||||
/** NextAuth 内置 session cookie 名(v5 默认 httpOnly) */
|
// /** NextAuth 内置 session cookie 名(v5 默认 httpOnly) */
|
||||||
const SESSION_COOKIE_NAMES = [
|
// const SESSION_COOKIE_NAMES = [
|
||||||
"next-auth.session-token",
|
// "next-auth.session-token",
|
||||||
"__Secure-next-auth.session-token",
|
// "__Secure-next-auth.session-token",
|
||||||
];
|
// ];
|
||||||
|
|
||||||
/** proxy 入口日志(Node.js runtime 跑,pino 兼容)—— 排查 / 监控路由用 */
|
// /** proxy 入口日志(Node.js runtime 跑,pino 兼容)—— 排查 / 监控路由用 */
|
||||||
const log = new Logger("Proxy");
|
// const log = new Logger("Proxy");
|
||||||
|
|
||||||
function isAuthOnlyRoute(pathname: string): boolean {
|
// function isAuthOnlyRoute(pathname: string): boolean {
|
||||||
return (AUTH_ONLY_ROUTES as readonly string[]).includes(pathname);
|
// return (AUTH_ONLY_ROUTES as readonly string[]).includes(pathname);
|
||||||
}
|
// }
|
||||||
|
|
||||||
function isProtectedRoute(pathname: string): boolean {
|
// function isProtectedRoute(pathname: string): boolean {
|
||||||
return (PROTECTED_ROUTES as readonly string[]).includes(pathname);
|
// return (PROTECTED_ROUTES as readonly string[]).includes(pathname);
|
||||||
}
|
// }
|
||||||
|
|
||||||
/**
|
// /**
|
||||||
* Proxy 入口
|
// * Proxy 入口
|
||||||
*/
|
// */
|
||||||
export function proxy(request: NextRequest) {
|
// export function proxy(request: NextRequest) {
|
||||||
const { pathname } = request.nextUrl;
|
// const { pathname } = request.nextUrl;
|
||||||
|
|
||||||
// 检查 NextAuth session cookie 存在性
|
// // 检查 NextAuth session cookie 存在性
|
||||||
const hasSession = SESSION_COOKIE_NAMES.some(
|
// const hasSession = SESSION_COOKIE_NAMES.some(
|
||||||
(name) => Boolean(request.cookies.get(name)?.value),
|
// (name) => Boolean(request.cookies.get(name)?.value),
|
||||||
);
|
// );
|
||||||
|
|
||||||
// 已登录访问未登录专属页 → /chat
|
// // 已登录访问未登录专属页 → /chat
|
||||||
if (hasSession && isAuthOnlyRoute(pathname)) {
|
// if (hasSession && isAuthOnlyRoute(pathname)) {
|
||||||
log.info(
|
// log.info(
|
||||||
{ pathname, target: ROUTES.chat, reason: "logged-in → auth-only" },
|
// { pathname, target: ROUTES.chat, reason: "logged-in → auth-only" },
|
||||||
"[proxy] redirect",
|
// "[proxy] redirect",
|
||||||
);
|
// );
|
||||||
const url = request.nextUrl.clone();
|
// const url = request.nextUrl.clone();
|
||||||
url.pathname = ROUTES.chat;
|
// url.pathname = ROUTES.chat;
|
||||||
return NextResponse.redirect(url, 308);
|
// return NextResponse.redirect(url, 308);
|
||||||
}
|
// }
|
||||||
|
|
||||||
// 未登录访问登录态专属页 → /splash(首界面)
|
// // 未登录访问登录态专属页 → /splash(首界面)
|
||||||
if (!hasSession && isProtectedRoute(pathname)) {
|
// if (!hasSession && isProtectedRoute(pathname)) {
|
||||||
log.info(
|
// log.info(
|
||||||
{ pathname, target: ROUTES.splash, reason: "not-logged-in → protected" },
|
// { pathname, target: ROUTES.splash, reason: "not-logged-in → protected" },
|
||||||
"[proxy] redirect",
|
// "[proxy] redirect",
|
||||||
);
|
// );
|
||||||
const url = request.nextUrl.clone();
|
// const url = request.nextUrl.clone();
|
||||||
url.pathname = ROUTES.splash;
|
// url.pathname = ROUTES.splash;
|
||||||
return NextResponse.redirect(url, 308);
|
// return NextResponse.redirect(url, 308);
|
||||||
}
|
// }
|
||||||
|
|
||||||
return NextResponse.next();
|
// return NextResponse.next();
|
||||||
}
|
// }
|
||||||
|
|
||||||
/**
|
// /**
|
||||||
* 匹配器:排除 API、Next 静态资源、图标与常见静态文件扩展名。
|
// * 匹配器:排除 API、Next 静态资源、图标与常见静态文件扩展名。
|
||||||
* 不排除 `_next/data`,因为 proxy 文档明确说明"即使在排除列表中 proxy 仍会跑"
|
// * 不排除 `_next/data`,因为 proxy 文档明确说明"即使在排除列表中 proxy 仍会跑"
|
||||||
* (安全兜底)。
|
// * (安全兜底)。
|
||||||
*/
|
// */
|
||||||
export const config = {
|
// export const config = {
|
||||||
matcher: [
|
// matcher: [
|
||||||
"/((?!api|_next/static|_next/image|_next/data|favicon.ico|icons|.*\\.(?:png|jpg|jpeg|gif|svg|webp|ico|css|js|woff2?|ttf|otf)$).*)",
|
// "/((?!api|_next/static|_next/image|_next/data|favicon.ico|icons|.*\\.(?:png|jpg|jpeg|gif|svg|webp|ico|css|js|woff2?|ttf|otf)$).*)",
|
||||||
],
|
// ],
|
||||||
};
|
// };
|
||||||
|
|||||||
@@ -8,6 +8,10 @@
|
|||||||
* - `as const` 让每个值都是字符串字面量类型,可用于 `PageProps<'/literal'>`。
|
* - `as const` 让每个值都是字符串字面量类型,可用于 `PageProps<'/literal'>`。
|
||||||
* - 不导出 Client-only 模块(`use-auth-gate` 走独立路径导入),保证 Server bundle 不被污染。
|
* - 不导出 Client-only 模块(`use-auth-gate` 走独立路径导入),保证 Server bundle 不被污染。
|
||||||
* - 动态深链通过 `ROUTE_BUILDERS` 函数生成,避免手拼字符串。
|
* - 动态深链通过 `ROUTE_BUILDERS` 函数生成,避免手拼字符串。
|
||||||
|
*
|
||||||
|
* 注意:proxy 路由跳转逻辑(2026-06-15 迁出),但本文件的**类型化常量**仍在
|
||||||
|
* 被 9 个组件(auth-screen / chat-header / quota-dialog / DeepLinkPersist /
|
||||||
|
* error / not-found / page / sidebar-screen / splash-screen)使用 —— **不能**整体注释。
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/** 静态路由字面量 */
|
/** 静态路由字面量 */
|
||||||
|
|||||||
Reference in New Issue
Block a user