ChatGPT-Next-Web/middleware.ts
2023-12-19 19:03:35 +08:00

66 lines
2.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { getToken } from "next-auth/jwt";
export default async function middleware(req: NextRequest) {
const url = req.nextUrl;
const searchParams = req.nextUrl.searchParams.toString();
const path = `${url.pathname}${
searchParams.length > 0 ? `?${searchParams}` : ""
}`;
const session = await getToken({ req });
// console.log('==============,认证,', path, session)
if (!session && path !== "/login") {
// 给关键请求特殊待遇
if (path.startsWith('/api/openai/')) {
return NextResponse.json(false, {
status: 401,
});
}
return NextResponse.redirect(new URL("/login", req.url));
} else if (session) {
if (path.startsWith("/login") || path.startsWith('/app/login')) return NextResponse.redirect(new URL("/", req.url));
}
if (path == '/login') {
return NextResponse.rewrite(
new URL(`/app${path}`, req.url),
);
}
if (path == "/admin") {
return NextResponse.rewrite(
new URL(`/app${path}`, req.url),
);
}
if (req.method == 'POST' && (path.startsWith("/api/openai/") || path.startsWith("/api/midjourney"))) {
// 重写header添加用户名
// console.log(session,'========')
const requestHeaders = new Headers(req.headers)
// 使用 encodeURIComponent 对特殊字符进行编码
// 将编码的 URI 组件转换成 Base64
const encodeName = Buffer.from(encodeURIComponent(`${session?.name}`)).toString('base64');
requestHeaders.set('x-request-name', encodeName)
return NextResponse.next({
request: {
// New request headers
headers: requestHeaders,
},
})
}
return NextResponse.next()
}
export const config = {
matcher: [
// "/api/:path*",
"/((?!api/logs/|api/auth/|_next/|_static/|_vercel|[\\w-]+\\.\\w+).*)",
],
};