forked from XiaoMo/ChatGPT-Next-Web
56 lines
1.3 KiB
TypeScript
56 lines
1.3 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { ACCESS_CODES } from "./app/api/access";
|
|
import md5 from "spark-md5";
|
|
|
|
export const config = {
|
|
matcher: ["/api/chat", "/api/chat-stream"],
|
|
};
|
|
|
|
export function middleware(req: NextRequest) {
|
|
const accessCode = req.headers.get("access-code");
|
|
const token = req.headers.get("token");
|
|
const hashedCode = md5.hash(accessCode ?? "").trim();
|
|
|
|
console.log("[Auth] allowed hashed codes: ", [...ACCESS_CODES]);
|
|
console.log("[Auth] got access code:", accessCode);
|
|
console.log("[Auth] hashed access code:", hashedCode);
|
|
|
|
if (ACCESS_CODES.size > 0 && !ACCESS_CODES.has(hashedCode) && !token) {
|
|
return NextResponse.json(
|
|
{
|
|
error: true,
|
|
needAccessCode: true,
|
|
msg: "Please go settings page and fill your access code.",
|
|
},
|
|
{
|
|
status: 401,
|
|
},
|
|
);
|
|
}
|
|
|
|
// inject api key
|
|
if (!token) {
|
|
const apiKey = process.env.OPENAI_API_KEY;
|
|
console.log("apiKey", apiKey);
|
|
if (apiKey) {
|
|
req.headers.set("token", apiKey);
|
|
} else {
|
|
return NextResponse.json(
|
|
{
|
|
error: true,
|
|
msg: "Empty Api Key",
|
|
},
|
|
{
|
|
status: 401,
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
return NextResponse.next({
|
|
request: {
|
|
headers: req.headers,
|
|
},
|
|
});
|
|
}
|