mirror of
https://github.com/MoonTechLab/LunaTV.git
synced 2026-03-12 08:47:28 +08:00
fix: middleware
This commit is contained in:
@@ -24,7 +24,7 @@ COPY . .
|
|||||||
|
|
||||||
# 在构建阶段也显式设置 DOCKER_ENV,
|
# 在构建阶段也显式设置 DOCKER_ENV,
|
||||||
# 确保 Next.js 在编译时即选择 Node Runtime 而不是 Edge Runtime
|
# 确保 Next.js 在编译时即选择 Node Runtime 而不是 Edge Runtime
|
||||||
RUN find ./src -type f -print0 \
|
RUN find ./src -type f -name "route.ts" -print0 \
|
||||||
| xargs -0 sed -i "s/export const runtime = 'edge';/export const runtime = 'nodejs';/g"
|
| xargs -0 sed -i "s/export const runtime = 'edge';/export const runtime = 'nodejs';/g"
|
||||||
ENV DOCKER_ENV=true
|
ENV DOCKER_ENV=true
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
import { NextRequest, NextResponse } from 'next/server';
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
|
|
||||||
export const runtime = 'edge';
|
|
||||||
|
|
||||||
// 全站(含 /api)鉴权中间件,运行于 Edge Runtime。
|
// 全站(含 /api)鉴权中间件,运行于 Edge Runtime。
|
||||||
export async function middleware(req: NextRequest) {
|
export async function middleware(req: NextRequest) {
|
||||||
const { pathname, search } = req.nextUrl;
|
const { pathname, search } = req.nextUrl;
|
||||||
@@ -20,12 +18,18 @@ export async function middleware(req: NextRequest) {
|
|||||||
return NextResponse.next();
|
return NextResponse.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 内部请求标记,避免递归拦截
|
||||||
|
if (req.headers.get('x-internal-auth') === 'true') {
|
||||||
|
return NextResponse.next();
|
||||||
|
}
|
||||||
|
|
||||||
// 通过后端接口验证登录状态(GET /api/login)
|
// 通过后端接口验证登录状态(GET /api/login)
|
||||||
const origin = req.nextUrl.origin;
|
const origin = req.nextUrl.origin;
|
||||||
const verifyRes = await fetch(`${origin}/api/login`, {
|
const verifyRes = await fetch(`${origin}/api/login`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
Cookie: req.headers.get('cookie') || '',
|
Cookie: req.headers.get('cookie') || '',
|
||||||
|
'x-internal-auth': 'true',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -47,6 +51,6 @@ export async function middleware(req: NextRequest) {
|
|||||||
// 2. 指定哪些路径使用 middleware
|
// 2. 指定哪些路径使用 middleware
|
||||||
export const config = {
|
export const config = {
|
||||||
matcher: [
|
matcher: [
|
||||||
'/((?!_next/static|_next/image|favicon.ico|manifest.json|icons|logo.png|screenshot.png).*)',
|
'/((?!_next/static|_next/image|favicon.ico|manifest.json|icons|logo.png|screenshot.png|api/login).*)',
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user