diff --git a/chatdesk-ui/app/[locale]/login/page.tsx b/chatdesk-ui/app/[locale]/login/page.tsx index a03788e..938a8e3 100644 --- a/chatdesk-ui/app/[locale]/login/page.tsx +++ b/chatdesk-ui/app/[locale]/login/page.tsx @@ -14,6 +14,7 @@ import { getRuntimeEnv } from "@/lib/ipconfig" // 新增引入 import { PostgrestError } from '@supabase/supabase-js'; import initTranslations from "@/lib/i18n"; +import { headers } from 'next/headers'; export const metadata: Metadata = { title: "Login" @@ -44,32 +45,30 @@ export default async function Login({ .eq("user_id", session.user.id) .eq("is_home", true) .maybeSingle(); - - // // Define the clearCookies function for server-side execution - // const clearCookies = async () => { - // // From request headers or environment variables - // let hosturl: string | undefined; - - // // Option 1: Get host from request headers (useful for dynamic requests) - // const hostHeader = req.headers.get('host'); - // if (hostHeader) { - // hosturl = `http://${hostHeader}`; // Construct full URL - // } else { - // // Option 2: Use environment variable as fallback - // hosturl = process.env.SUPABASE_URL; // Ensure you have this set up in your environment variables - // } - - // if (hosturl) { - // await fetch(`${hosturl}/api/clearcookies`, { - // method: "POST", - // }); - // } else { - // console.error("Host URL is not available"); - // } - // }; if (!homeWorkspace) { - //await clearCookies(); // Clear cookies if home workspace is not found + + + // 获取 host 值,根据环境不同决定获取方式 + let hosturl: string | undefined; + + if (typeof window !== "undefined") { + // 客户端环境下,使用 window.location.host + hosturl = `http://${window.location.host}`; + } else { + // 服务器端环境下,使用 next/headers 获取 host + const hostHeader = headers().get('host'); + hosturl = hostHeader ? `http://${hostHeader}` : process.env.SUPABASE_URL; + } + + if (hosturl) { + await fetch(`${hosturl}/api/clearCookies`, { + method: "POST", + }); + } else { + console.error("Host URL is not available"); + } + return redirect(`/${localeString}/login?message=sessionExpired`); } return redirect(`/${localeString}/${homeWorkspace.id}/chat`);