From af1f47f4f0865032c4bb9a5942e370061645d860 Mon Sep 17 00:00:00 2001 From: shinya Date: Sat, 26 Jul 2025 21:10:09 +0800 Subject: [PATCH] feat: add role in authInfo --- src/app/api/login/route.ts | 24 ++++++++++++++++++++---- src/app/api/register/route.ts | 1 + src/lib/auth.ts | 1 + 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/app/api/login/route.ts b/src/app/api/login/route.ts index 556d07f..9c8989e 100644 --- a/src/app/api/login/route.ts +++ b/src/app/api/login/route.ts @@ -45,9 +45,10 @@ async function generateSignature( async function generateAuthCookie( username?: string, password?: string, + role?: 'owner' | 'admin' | 'user', includePassword = false ): Promise { - const authData: any = {}; + const authData: any = { role: role || 'user' }; // 只在需要时包含 password if (includePassword && password) { @@ -101,7 +102,12 @@ export async function POST(req: NextRequest) { // 验证成功,设置认证cookie const response = NextResponse.json({ ok: true }); - const cookieValue = await generateAuthCookie(undefined, password, true); // localstorage 模式包含 password + const cookieValue = await generateAuthCookie( + undefined, + password, + 'user', + true + ); // localstorage 模式包含 password const expires = new Date(); expires.setDate(expires.getDate() + 7); // 7天过期 @@ -133,7 +139,12 @@ export async function POST(req: NextRequest) { ) { // 验证成功,设置认证cookie const response = NextResponse.json({ ok: true }); - const cookieValue = await generateAuthCookie(username, password, false); // 数据库模式不包含 password + const cookieValue = await generateAuthCookie( + username, + password, + 'owner', + false + ); // 数据库模式不包含 password const expires = new Date(); expires.setDate(expires.getDate() + 7); // 7天过期 @@ -168,7 +179,12 @@ export async function POST(req: NextRequest) { // 验证成功,设置认证cookie const response = NextResponse.json({ ok: true }); - const cookieValue = await generateAuthCookie(username, password, false); // 数据库模式不包含 password + const cookieValue = await generateAuthCookie( + username, + password, + user?.role || 'user', + false + ); // 数据库模式不包含 password const expires = new Date(); expires.setDate(expires.getDate() + 7); // 7天过期 diff --git a/src/app/api/register/route.ts b/src/app/api/register/route.ts index 30ed815..8683e2b 100644 --- a/src/app/api/register/route.ts +++ b/src/app/api/register/route.ts @@ -44,6 +44,7 @@ async function generateSignature( // 生成认证Cookie(带签名) async function generateAuthCookie(username: string): Promise { const authData: any = { + role: 'user', username, timestamp: Date.now(), }; diff --git a/src/lib/auth.ts b/src/lib/auth.ts index ea11ad3..463ae9e 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -28,6 +28,7 @@ export function getAuthInfoFromBrowserCookie(): { username?: string; signature?: string; timestamp?: number; + role?: 'owner' | 'admin' | 'user'; } | null { if (typeof window === 'undefined') { return null;