This commit is contained in:
parent
b206be4e70
commit
a0fd937b79
|
|
@ -13,6 +13,7 @@ import "./globals.css"
|
|||
import { getRuntimeEnv } from "@/lib/ipconfig" // 新增引入
|
||||
import Script from "next/script"
|
||||
|
||||
|
||||
const inter = Inter({ subsets: ["latin"] })
|
||||
const APP_NAME = "ChatDesk UI"
|
||||
const APP_DEFAULT_TITLE = "ChatDesk UI"
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import { Metadata } from "next"
|
|||
import { cookies, headers } from "next/headers"
|
||||
import { redirect } from "next/navigation"
|
||||
import { getRuntimeEnv } from "@/lib/ipconfig" // 新增引入
|
||||
import { initSupabase } from "@/lib/supabase/init"
|
||||
|
||||
|
||||
import initTranslations from "@/lib/i18n";
|
||||
|
|
@ -30,17 +31,19 @@ export default async function Login({
|
|||
const localeString = locale;
|
||||
const { t, resources } = await initTranslations(localeString, ['translation']);
|
||||
|
||||
const supabase = createServerClient<Database>(
|
||||
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<Database>(
|
||||
// getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000",
|
||||
// process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
|
||||
// {
|
||||
// cookies: {
|
||||
// get(name: string) {
|
||||
// return cookieStore.get(name)?.value
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// )
|
||||
const supabase = await initSupabase()
|
||||
|
||||
const session = (await supabase.auth.getSession()).data.session
|
||||
|
||||
console.log("[login page]Login session:", session)
|
||||
|
|
|
|||
|
|
@ -1,8 +1,54 @@
|
|||
import { Database } from "@/supabase/types"
|
||||
import { createBrowserClient } from "@supabase/ssr"
|
||||
import { getRuntimeEnv } from "@/lib/ipconfig" // 新增引入
|
||||
import { createBrowserClient, createServerClient } from "@supabase/ssr"
|
||||
import { cookies as serverCookies } from "next/headers"
|
||||
|
||||
|
||||
export const supabase = createBrowserClient<Database>(
|
||||
getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000",
|
||||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
|
||||
)
|
||||
|
||||
|
||||
let _supabase: ReturnType<typeof createBrowserClient> | null = null
|
||||
|
||||
export async function initSupabase(providedCookieStore?: ReturnType<typeof serverCookies>) {
|
||||
if (typeof window === "undefined") {
|
||||
// SSR 路径:用服务端 Supabase 客户端
|
||||
const cookieStore = providedCookieStore ?? serverCookies()
|
||||
return createServerClient<Database>(
|
||||
getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000",
|
||||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
|
||||
{
|
||||
cookies: {
|
||||
get(name: string) {
|
||||
return cookieStore.get(name)?.value
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
// CSR 路径:等待 RUNTIME_ENV 注入,并复用客户端实例
|
||||
if (_supabase) return _supabase
|
||||
|
||||
await waitForRuntimeEnv()
|
||||
|
||||
const url = window.RUNTIME_ENV?.SUPABASE_URL
|
||||
const key = window.RUNTIME_ENV?.SUPABASE_ANON_KEY
|
||||
|
||||
if (!url || !key) {
|
||||
throw new Error("Missing SUPABASE_URL or SUPABASE_ANON_KEY in RUNTIME_ENV")
|
||||
}
|
||||
|
||||
_supabase = createBrowserClient(url, key)
|
||||
return _supabase
|
||||
}
|
||||
|
||||
async function waitForRuntimeEnv(retry = 50, interval = 50) {
|
||||
for (let i = 0; i < retry; i++) {
|
||||
if (window.RUNTIME_ENV?.SUPABASE_URL && window.RUNTIME_ENV?.SUPABASE_ANON_KEY) return
|
||||
await new Promise((r) => setTimeout(r, interval))
|
||||
}
|
||||
throw new Error("window.RUNTIME_ENV not ready after wait")
|
||||
}
|
||||
|
|
@ -1,8 +1,13 @@
|
|||
import { createBrowserClient } from "@supabase/ssr"
|
||||
import { getRuntimeEnv } from "@/lib/ipconfig" // 新增引入
|
||||
// import { createBrowserClient } from "@supabase/ssr"
|
||||
// import { getRuntimeEnv } from "@/lib/ipconfig" // 新增引入
|
||||
|
||||
export const createClient = () =>
|
||||
createBrowserClient(
|
||||
getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000",
|
||||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
|
||||
)
|
||||
// export const createClient = () =>
|
||||
// createBrowserClient(
|
||||
// getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000",
|
||||
// process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
|
||||
// )
|
||||
|
||||
|
||||
import { initSupabase } from "@/lib/supabase/init"
|
||||
|
||||
export const createClient = initSupabase
|
||||
Loading…
Reference in New Issue