This commit is contained in:
hailin 2025-05-21 00:17:17 +08:00
parent 7487bf9b71
commit 3a7f12f716
4 changed files with 115 additions and 71 deletions

View File

@ -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<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 = getSupabaseServerClient()
// const session = (await supabase.auth.getSession()).data.session
const { data, error } = await supabase.auth.getSession();
if (error) {

View File

@ -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<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 = getSupabaseServerClient()
const session = (await supabase.auth.getSession()).data.session
console.log("[login page]Login session:", session)

View File

@ -10,23 +10,26 @@
let _env: Record<string, string> | 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
}

View File

@ -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<typeof cookies>) => {
// 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<typeof cookies>) => {
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<Database>(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 (_) {}
}
}
)
})
}