From 16abb64d3c161f59050a86df549e2352af34c98e Mon Sep 17 00:00:00 2001 From: hailin Date: Thu, 22 May 2025 19:37:20 +0800 Subject: [PATCH] . --- chatdesk-ui/app/[locale]/login/page.tsx | 3 - chatdesk-ui/app/[locale]/setup/page.tsx | 8 --- chatdesk-ui/app/api/clearcookies/route.js | 87 +++++++++++++---------- chatdesk-ui/lib/ipconfig.ts | 2 +- 4 files changed, 52 insertions(+), 48 deletions(-) diff --git a/chatdesk-ui/app/[locale]/login/page.tsx b/chatdesk-ui/app/[locale]/login/page.tsx index 16cf41f..59f5de9 100644 --- a/chatdesk-ui/app/[locale]/login/page.tsx +++ b/chatdesk-ui/app/[locale]/login/page.tsx @@ -136,7 +136,6 @@ export default async function Login({ const signUp = async (formData: FormData) => { "use server" - const email = formData.get("email") as string const password = formData.get("password") as string @@ -165,8 +164,6 @@ export default async function Login({ } } - //const cookieStore = cookies() - //const supabase = createClient(cookieStore) const supabase = getSupabaseServerClient() const { error } = await supabase.auth.signUp({ email, diff --git a/chatdesk-ui/app/[locale]/setup/page.tsx b/chatdesk-ui/app/[locale]/setup/page.tsx index 577bad8..e55c9c3 100644 --- a/chatdesk-ui/app/[locale]/setup/page.tsx +++ b/chatdesk-ui/app/[locale]/setup/page.tsx @@ -54,18 +54,10 @@ export default function SetupPage() { if (locales.includes(segment)) { locale = segment } - // /zh ok - //const homePath = locale === defaultLocale ? "/" : `/${locale}` - // en ok - //const homePath = locale === defaultLocale ? "" : `/${locale}` const getLocalizedPath = (subPath: string) => locale === defaultLocale ? `/${subPath}` : `/${locale}/${subPath}` - - // // 提取当前路径中的 locale 部分 - // const locale = pathname.split("/")[1] || "en" // 获取路径中的 locale 部分,如果没有则默认为 "en" - const { t } = useTranslation() const [loading, setLoading] = useState(true) diff --git a/chatdesk-ui/app/api/clearcookies/route.js b/chatdesk-ui/app/api/clearcookies/route.js index fd54b51..b8398a5 100644 --- a/chatdesk-ui/app/api/clearcookies/route.js +++ b/chatdesk-ui/app/api/clearcookies/route.js @@ -1,57 +1,72 @@ -import { cookies, headers } from "next/headers"; -import { NextResponse } from 'next/server'; +// import { cookies, headers } from "next/headers"; +// import { NextResponse } from 'next/server'; + +// export async function POST() { +// const cookieStore = cookies(); +// const allCookies = cookieStore.getAll(); + +// console.log("Clearing the following cookies:"); + +// // 获取当前请求的 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); +// } +// }); + +// // Return success response using NextResponse +// return NextResponse.json({ message: "Cookies cleared successfully" }, { status: 200 }); +// } + + +import { cookies } from "next/headers"; +import { NextResponse } from "next/server"; export async function POST() { const cookieStore = cookies(); const allCookies = cookieStore.getAll(); - console.log("Clearing the following cookies:"); + console.log("🔍 Clearing the following cookies:"); + allCookies.forEach(({ name }) => console.log(`- ${name}`)); - // 获取当前请求的 host(服务器端) - const hostHeader = headers().get("host"); - const currentHost = hostHeader || "localhost"; // 如果没有 host 信息,使用 "localhost" + const response = NextResponse.json( + { message: "Cookies cleared successfully" }, + { status: 200 } + ); - // Loop through all cookies and clear them + // Clear each cookie by setting an expired value allCookies.forEach(({ name }) => { - console.log(`............Clearing cookie: ${name}`); try { - cookieStore.set({ + response.cookies.set({ name, value: "", path: "/", maxAge: 0, httpOnly: true, - secure: true, + secure: process.env.NODE_ENV === "production", // ← 自动判断环境 sameSite: "lax", - domain: currentHost, // 使用服务器端获取的 host + // No domain — will auto match current domain }); } catch (error) { - console.error(`Error clearing cookie ${name}:`, error); + console.error(`❌ Error clearing cookie ${name}:`, error); } }); - // Return success response using NextResponse - return NextResponse.json({ message: "Cookies cleared successfully" }, { status: 200 }); + return response; } - - - - - -// import { NextResponse } from 'next/server'; - -// export async function POST(req) { -// try { -// console.log("Clearing cookies..."); - -// // Perform cookie clearing logic here - -// // Return success response using NextResponse -// return NextResponse.json({ message: "Cookies cleared successfully" }, { status: 200 }); -// } catch (error) { -// console.error("Error occurred while clearing cookies:", error); - -// // Return error response -// return NextResponse.json({ error: "Internal server error" }, { status: 500 }); -// } -// } diff --git a/chatdesk-ui/lib/ipconfig.ts b/chatdesk-ui/lib/ipconfig.ts index 6e269e9..0969731 100644 --- a/chatdesk-ui/lib/ipconfig.ts +++ b/chatdesk-ui/lib/ipconfig.ts @@ -61,7 +61,7 @@ export function getRuntimeEnv(key: string): string | undefined { return _env[key] } - console.log("[browser-side] No window.RUNTIME_ENV found in browser") + console.log("[browser-side] No window.RUNTIME_ENV found in browser, _env=", _env) let retries = 0 // 定义一个同步延时重试的函数