From 3a7f12f71631c2cf1ecebefaba8c72b9d4cdf688 Mon Sep 17 00:00:00 2001 From: hailin Date: Wed, 21 May 2025 00:17:17 +0800 Subject: [PATCH] . --- chatdesk-ui/app/[locale]/layout.tsx | 27 +++---- chatdesk-ui/app/[locale]/login/page.tsx | 29 ++++---- chatdesk-ui/lib/ipconfig.ts | 37 +++++----- chatdesk-ui/lib/supabase/server.ts | 93 +++++++++++++++++-------- 4 files changed, 115 insertions(+), 71 deletions(-) diff --git a/chatdesk-ui/app/[locale]/layout.tsx b/chatdesk-ui/app/[locale]/layout.tsx index 3a3c4f1..dd907f4 100644 --- a/chatdesk-ui/app/[locale]/layout.tsx +++ b/chatdesk-ui/app/[locale]/layout.tsx @@ -4,7 +4,8 @@ import { Providers } from "@/components/utility/providers" import TranslationsProvider from "@/components/utility/translations-provider" import initTranslations from "@/lib/i18n" import { Database } from "@/supabase/types" -import { createServerClient } from "@supabase/ssr" +//import { createServerClient } from "@supabase/ssr" +import { getSupabaseServerClient } from "@/lib/supabase/server" import { Metadata, Viewport } from "next" import { Inter } from "next/font/google" import { cookies } from "next/headers" @@ -96,17 +97,19 @@ export default async function RootLayout({ const supabaseUrl = getRuntimeEnv("SUPABASE_URL"); console.log("=================>Server's Supabase URL: ", supabaseUrl); - const supabase = createServerClient( - getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000", - process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, - { - cookies: { - get(name: string) { - return cookieStore.get(name)?.value - } - } - } - ) + // const supabase = createServerClient( + // getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000", + // process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, + // { + // cookies: { + // get(name: string) { + // return cookieStore.get(name)?.value + // } + // } + // } + // ) + + const supabase = getSupabaseServerClient() // const session = (await supabase.auth.getSession()).data.session const { data, error } = await supabase.auth.getSession(); if (error) { diff --git a/chatdesk-ui/app/[locale]/login/page.tsx b/chatdesk-ui/app/[locale]/login/page.tsx index b42f454..151218b 100644 --- a/chatdesk-ui/app/[locale]/login/page.tsx +++ b/chatdesk-ui/app/[locale]/login/page.tsx @@ -2,9 +2,10 @@ import { Brand } from "@/components/ui/brand" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { SubmitButton } from "@/components/ui/submit-button" -import { createClient } from "@/lib/supabase/server" +//import { createClient } from "@/lib/supabase/server" import { Database } from "@/supabase/types" -import { createServerClient } from "@supabase/ssr" +//import { createServerClient } from "@supabase/ssr" +import { getSupabaseServerClient } from "@/lib/supabase/server" import { get } from "@vercel/edge-config" import { Metadata } from "next" import { cookies, headers } from "next/headers" @@ -30,18 +31,20 @@ export default async function Login({ const localeString = locale; const { t, resources } = await initTranslations(localeString, ['translation']); - const supabase = createServerClient( - getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000", - process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, - { - cookies: { - get(name: string) { - return cookieStore.get(name)?.value - } - } - } - ) + // const supabase = createServerClient( + // getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000", + // process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, + // { + // cookies: { + // get(name: string) { + // return cookieStore.get(name)?.value + // } + // } + // } + // ) + const supabase = getSupabaseServerClient() + const session = (await supabase.auth.getSession()).data.session console.log("[login page]Login session:", session) diff --git a/chatdesk-ui/lib/ipconfig.ts b/chatdesk-ui/lib/ipconfig.ts index 9c18cda..c2e4b5a 100644 --- a/chatdesk-ui/lib/ipconfig.ts +++ b/chatdesk-ui/lib/ipconfig.ts @@ -10,23 +10,26 @@ let _env: Record | null = null export function getRuntimeEnv(key: string): string | undefined { - // 打印缓存的 _env 变量和每次调用时的 key - console.log("Checking env for key:", key); - - // 如果 _env 存在,打印出缓存的 _env 并返回对应的值 - if (_env) { - console.log("Returning cached _env:", _env); - return _env[key] + console.log("Checking env for key:", key) + + if (typeof window !== "undefined") { + if (!_env && typeof window.RUNTIME_ENV !== "undefined") { + _env = window.RUNTIME_ENV + console.log("Updated _env from window.RUNTIME_ENV:", _env) + } + + if (_env) { + console.log("Returning cached _env:", _env) + return _env[key] + } + + console.log("No window.RUNTIME_ENV found in browser") + return undefined } - // 如果 window.RUNTIME_ENV 存在,打印出从 window.RUNTIME_ENV 获取的值 - if (typeof window !== "undefined" && typeof window.RUNTIME_ENV !== "undefined") { - _env = window.RUNTIME_ENV // 更新 _env 为最新的环境变量 - console.log("Updated _env from window.RUNTIME_ENV:", _env); - return _env[key] - } - - // 如果没有 window.RUNTIME_ENV,回退到 process.env,打印出 process.env 和当前 key - console.log("Falling back to process.env for key:", key); - return process.env[key] + // 服务端始终动态读取 process.env + const val = process.env[key] + console.log("Falling back to process.env for key:", key, "value:", val) + return val } + diff --git a/chatdesk-ui/lib/supabase/server.ts b/chatdesk-ui/lib/supabase/server.ts index 948907a..2cf3459 100644 --- a/chatdesk-ui/lib/supabase/server.ts +++ b/chatdesk-ui/lib/supabase/server.ts @@ -1,35 +1,70 @@ +// import { createServerClient, type CookieOptions } from "@supabase/ssr" +// import { cookies } from "next/headers" +// import { getRuntimeEnv } from "@/lib/ipconfig" // 新增引入 + +// export const createClient = (cookieStore: ReturnType) => { +// return createServerClient( +// getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000", +// process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, +// { +// cookies: { +// get(name: string) { +// return cookieStore.get(name)?.value +// }, +// set(name: string, value: string, options: CookieOptions) { +// try { +// cookieStore.set({ name, value, ...options }) +// } catch (error) { +// // The `set` method was called from a Server Component. +// // This can be ignored if you have middleware refreshing +// // user sessions. +// } +// }, +// remove(name: string, options: CookieOptions) { +// try { +// cookieStore.set({ name, value: "", ...options }) +// } catch (error) { +// // The `delete` method was called from a Server Component. +// // This can be ignored if you have middleware refreshing +// // user sessions. +// } +// } +// } +// } +// ) +// } + + + + + + import { createServerClient, type CookieOptions } from "@supabase/ssr" import { cookies } from "next/headers" -import { getRuntimeEnv } from "@/lib/ipconfig" // 新增引入 +import { getRuntimeEnv } from "@/lib/ipconfig" +import { Database } from "@/supabase/types" -export const createClient = (cookieStore: ReturnType) => { - return createServerClient( - getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000", - process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, - { - cookies: { - get(name: string) { - return cookieStore.get(name)?.value - }, - set(name: string, value: string, options: CookieOptions) { - try { - cookieStore.set({ name, value, ...options }) - } catch (error) { - // The `set` method was called from a Server Component. - // This can be ignored if you have middleware refreshing - // user sessions. - } - }, - remove(name: string, options: CookieOptions) { - try { - cookieStore.set({ name, value: "", ...options }) - } catch (error) { - // The `delete` method was called from a Server Component. - // This can be ignored if you have middleware refreshing - // user sessions. - } - } +export function getSupabaseServerClient() { + const supabaseUrl = getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000" + const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY! + + const cookieStore = cookies() + + return createServerClient(supabaseUrl, supabaseAnonKey, { + cookies: { + get(name: string) { + return cookieStore.get(name)?.value + }, + set(name: string, value: string, options: CookieOptions) { + try { + cookieStore.set({ name, value, ...options }) + } catch (_) {} + }, + remove(name: string, options: CookieOptions) { + try { + cookieStore.set({ name, value: "", ...options }) + } catch (_) {} } } - ) + }) }