Your Name 53ddb7e585
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
Initial commit - clean repository
2025-12-17 20:15:18 +08:00

48 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NextResponse } from "next/server";
export async function POST() {
const isSecure = process.env.AUTH_URL?.startsWith("https://") ?? false;
const domain = process.env.AUTH_URL ? new URL(process.env.AUTH_URL).hostname : undefined;
// 创建清除 cookie 的响应
const response = NextResponse.json({ success: true });
// 清除所有认证相关的 cookie
const cookiesToClear = [
"__Secure-authjs.session-token",
"__Secure-authjs.callback-url",
"__Host-authjs.csrf-token",
"authjs.session-token",
"authjs.callback-url",
"authjs.csrf-token",
];
for (const cookieName of cookiesToClear) {
// 清除不带 domain 的 cookie
response.cookies.set(cookieName, "", {
expires: new Date(0),
path: "/",
secure: isSecure,
httpOnly: cookieName.includes("session-token") || cookieName.includes("csrf-token"),
sameSite: "lax",
});
// 如果有 domain,也清除带 domain 的 cookie
if (domain && domain !== "localhost" && !cookieName.startsWith("__Host-")) {
response.cookies.set(cookieName, "", {
expires: new Date(0),
path: "/",
domain: domain,
secure: isSecure,
httpOnly: cookieName.includes("session-token") || cookieName.includes("csrf-token"),
sameSite: "lax",
});
}
}
console.log("[Logout API] Cleared all auth cookies");
return response;
}