This commit is contained in:
hailin 2025-05-21 18:18:52 +08:00
parent 80fa6fba00
commit bbca756b9b
1 changed files with 42 additions and 27 deletions

View File

@ -1,33 +1,48 @@
import { cookies, headers } from "next/headers";
// import { cookies, headers } from "next/headers";
export async function POST() {
const cookieStore = cookies();
const allCookies = cookieStore.getAll();
// export async function POST() {
// const cookieStore = cookies();
// const allCookies = cookieStore.getAll();
console.log("Clearing the following cookies:");
// console.log("Clearing the following cookies:");
// 获取当前请求的 host服务器端
const hostHeader = headers().get("host");
const currentHost = hostHeader || "localhost"; // 如果没有 host 信息,使用 "localhost"
// // 获取当前请求的 host服务器端
// const hostHeader = headers().get("host");
// const currentHost = hostHeader || "localhost"; // 如果没有 host 信息,使用 "localhost"
// Loop through all cookies and clear them
allCookies.forEach(({ name }) => {
console.log(`............Clearing cookie: ${name}`);
try {
cookieStore.set({
name,
value: "",
path: "/",
maxAge: 0,
httpOnly: true,
secure: true,
sameSite: "lax",
domain: currentHost, // 使用服务器端获取的 host
});
} catch (error) {
console.error(`Error clearing cookie ${name}:`, error);
}
});
// // Loop through all cookies and clear them
// allCookies.forEach(({ name }) => {
// console.log(`............Clearing cookie: ${name}`);
// try {
// cookieStore.set({
// name,
// value: "",
// path: "/",
// maxAge: 0,
// httpOnly: true,
// secure: true,
// sameSite: "lax",
// domain: currentHost, // 使用服务器端获取的 host
// });
// } catch (error) {
// console.error(`Error clearing cookie ${name}:`, error);
// }
// });
return new Response("Cookies cleared successfully", { status: 200 });
// return new Response("Cookies cleared successfully", { status: 200 });
// }
export async function POST(req, res) {
try {
// 模拟清除所有 cookies 的操作
console.log("Clearing all cookies...");
// 返回一个成功的响应
return res.status(200).json({ message: "Cookies cleared successfully" });
} catch (error) {
console.error("Error clearing cookies:", error);
return res.status(500).json({ error: "Failed to clear cookies" });
}
}