mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-10-09 11:36:38 +08:00
添加middleware
This commit is contained in:
parent
8239551e8e
commit
202fddd6e8
@ -31,55 +31,55 @@ export function middleware(req: NextRequest) {
|
|||||||
const [user, pwd] = atob(authValue).split(':')
|
const [user, pwd] = atob(authValue).split(':')
|
||||||
|
|
||||||
if (user === '4dmin' && pwd === 'testpwd123') {
|
if (user === '4dmin' && pwd === 'testpwd123') {
|
||||||
return NextResponse.next()
|
// return NextResponse.next()
|
||||||
|
console.log("[Auth] allowed hashed codes: ", [...serverConfig.codes]);
|
||||||
|
console.log("[Auth] got access code:", accessCode);
|
||||||
|
console.log("[Auth] hashed access code:", hashedCode);
|
||||||
|
console.log("[User IP] ", getIP(req));
|
||||||
|
console.log("[Time] ", new Date().toLocaleString());
|
||||||
|
|
||||||
|
if (serverConfig.needCode && !serverConfig.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 = serverConfig.apiKey;
|
||||||
|
if (apiKey) {
|
||||||
|
console.log("[Auth] set system token");
|
||||||
|
req.headers.set("token", apiKey);
|
||||||
|
} else {
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: true,
|
||||||
|
msg: "Empty Api Key",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: 401,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log("[Auth] set user token");
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.next({
|
||||||
|
request: {
|
||||||
|
headers: req.headers,
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
url.pathname = '/api/auth'
|
url.pathname = '/api/auth'
|
||||||
return NextResponse.rewrite(url)
|
return NextResponse.rewrite(url)
|
||||||
}
|
}
|
||||||
console.log("[Auth] allowed hashed codes: ", [...serverConfig.codes]);
|
|
||||||
console.log("[Auth] got access code:", accessCode);
|
|
||||||
console.log("[Auth] hashed access code:", hashedCode);
|
|
||||||
console.log("[User IP] ", getIP(req));
|
|
||||||
console.log("[Time] ", new Date().toLocaleString());
|
|
||||||
|
|
||||||
if (serverConfig.needCode && !serverConfig.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 = serverConfig.apiKey;
|
|
||||||
if (apiKey) {
|
|
||||||
console.log("[Auth] set system token");
|
|
||||||
req.headers.set("token", apiKey);
|
|
||||||
} else {
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error: true,
|
|
||||||
msg: "Empty Api Key",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
status: 401,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.log("[Auth] set user token");
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.next({
|
|
||||||
request: {
|
|
||||||
headers: req.headers,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user