feat: middleware

This commit is contained in:
shinya
2025-07-09 21:08:59 +08:00
parent 9416f40834
commit e42f830228
5 changed files with 29 additions and 24 deletions

View File

@@ -41,9 +41,15 @@ async function generateSignature(
// 生成认证Cookie带签名
async function generateAuthCookie(
username?: string,
password?: string
password?: string,
includePassword = false
): Promise<string> {
const authData: any = { password };
const authData: any = {};
// 只在需要时包含 password
if (includePassword && password) {
authData.password = password;
}
if (username && process.env.PASSWORD) {
authData.username = username;
@@ -90,7 +96,7 @@ export async function POST(req: NextRequest) {
// 验证成功设置认证cookie
const response = NextResponse.json({ ok: true });
const cookieValue = await generateAuthCookie(undefined, password);
const cookieValue = await generateAuthCookie(undefined, password, true); // localstorage 模式包含 password
const expires = new Date();
expires.setDate(expires.getDate() + 7); // 7天过期
@@ -120,7 +126,7 @@ export async function POST(req: NextRequest) {
) {
// 验证成功设置认证cookie
const response = NextResponse.json({ ok: true });
const cookieValue = await generateAuthCookie(username, password);
const cookieValue = await generateAuthCookie(username, password, false); // 数据库模式不包含 password
const expires = new Date();
expires.setDate(expires.getDate() + 7); // 7天过期
@@ -153,7 +159,7 @@ export async function POST(req: NextRequest) {
// 验证成功设置认证cookie
const response = NextResponse.json({ ok: true });
const cookieValue = await generateAuthCookie(username, password);
const cookieValue = await generateAuthCookie(username, password, false); // 数据库模式不包含 password
const expires = new Date();
expires.setDate(expires.getDate() + 7); // 7天过期

View File

@@ -39,12 +39,8 @@ async function generateSignature(
}
// 生成认证Cookie带签名
async function generateAuthCookie(
username: string,
password: string
): Promise<string> {
async function generateAuthCookie(username: string): Promise<string> {
const authData: any = {
password,
username,
timestamp: Date.now(),
};
@@ -105,7 +101,7 @@ export async function POST(req: NextRequest) {
// 注册成功设置认证cookie
const response = NextResponse.json({ ok: true });
const cookieValue = await generateAuthCookie(username, password);
const cookieValue = await generateAuthCookie(username);
const expires = new Date();
expires.setDate(expires.getDate() + 7); // 7天过期