refactor(middleware): 迁移 middleware 至 proxy(Next 16 弃用约定)
This commit is contained in:
+4
-3
@@ -8,7 +8,8 @@ interface JwtPayload {
|
||||
}
|
||||
|
||||
// ============ Edge-compatible JWT 验证 ============
|
||||
// Next.js Middleware 运行在 Edge Runtime,不能依赖 jsonwebtoken 的 Node crypto。
|
||||
// Next.js Proxy(原 Middleware 约定,Next 16 起重命名)运行在 Edge Runtime,
|
||||
// 不能依赖 jsonwebtoken 的 Node crypto。
|
||||
// 这里使用 Web Crypto API 实现 HS256 签名验证,与 API 路由中的 jsonwebtoken 共享同一密钥。
|
||||
|
||||
function base64urlToBuffer(value: string): Uint8Array {
|
||||
@@ -65,7 +66,7 @@ async function verifyAccessTokenEdge(token: string): Promise<JwtPayload> {
|
||||
return payload;
|
||||
}
|
||||
|
||||
// ============ Middleware ============
|
||||
// ============ Proxy(原 Middleware,Next 16 弃用 middleware 约定) ============
|
||||
|
||||
function redirectToLogin(request: NextRequest, pathname: string): NextResponse {
|
||||
const loginUrl = new URL('/admin/login', request.url);
|
||||
@@ -73,7 +74,7 @@ function redirectToLogin(request: NextRequest, pathname: string): NextResponse {
|
||||
return NextResponse.redirect(loginUrl);
|
||||
}
|
||||
|
||||
export async function middleware(request: NextRequest) {
|
||||
export async function proxy(request: NextRequest) {
|
||||
const { pathname } = request.nextUrl;
|
||||
|
||||
// 只处理 /admin 路由(除了登录页和 API 路由)
|
||||
|
||||
Reference in New Issue
Block a user