48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
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;
|
||
}
|
||
|