修复鉴权失败后跳转的问题

This commit is contained in:
2026-04-01 13:16:56 +08:00
parent 61b766d939
commit a9e9ddd04b
2 changed files with 9 additions and 17 deletions

View File

@@ -61,16 +61,8 @@ export async function logout() {
} }
// 删除 cookies // 删除 cookies
cookieStore.set("admin/auth_token", "", { cookieStore.delete("admin/auth_token")
httpOnly: true, cookieStore.delete("admin/auth_refresh")
sameSite: "strict",
maxAge: -1,
})
cookieStore.set("admin/auth_refresh", "", {
httpOnly: true,
sameSite: "strict",
maxAge: -1,
})
return { return {
success: true, success: true,

View File

@@ -33,15 +33,15 @@ export async function proxy(request: NextRequest) {
} }
// 验证访问令牌 // 验证访问令牌
const hasToken = !!request.cookies.get("admin/auth_token") const hasToken = request.cookies.has("admin/auth_token")
// const isToAdmin = request.nextUrl.pathname.startsWith("/admin") // const isToAdmin = request.nextUrl.pathname.startsWith("/admin")
const protectedPaths = ["/", "/admin"] const ignoredPaths = ["/login"]
const isProtectedPath = protectedPaths.some( const ignored = ignoredPaths.some(path =>
path => request.nextUrl.pathname.startsWith(path),
request.nextUrl.pathname === path ||
request.nextUrl.pathname.startsWith(`${path}/`),
) )
if (!hasToken && isProtectedPath) {
console.log("hasToken", hasToken, "ignored", ignored)
if (!hasToken && !ignored) {
return NextResponse.redirect( return NextResponse.redirect(
`${request.nextUrl.origin}/login?redirect=${request.nextUrl.pathname}`, `${request.nextUrl.origin}/login?redirect=${request.nextUrl.pathname}`,
) )