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 TranslationsProvider from "@/components/utility/translations-provider"
import initTranslations from "@/lib/i18n" import initTranslations from "@/lib/i18n"
import { Database } from "@/supabase/types" 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 { Metadata, Viewport } from "next"
import { Inter } from "next/font/google" import { Inter } from "next/font/google"
import { cookies } from "next/headers" import { cookies } from "next/headers"
@ -96,17 +97,19 @@ export default async function RootLayout({
const supabaseUrl = getRuntimeEnv("SUPABASE_URL"); const supabaseUrl = getRuntimeEnv("SUPABASE_URL");
console.log("=================>Server's Supabase URL: ", supabaseUrl); console.log("=================>Server's Supabase URL: ", supabaseUrl);
const supabase = createServerClient<Database>( // const supabase = createServerClient<Database>(
getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000", // getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000",
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, // process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{ // {
cookies: { // cookies: {
get(name: string) { // get(name: string) {
return cookieStore.get(name)?.value // return cookieStore.get(name)?.value
} // }
} // }
} // }
) // )
const supabase = getSupabaseServerClient()
// const session = (await supabase.auth.getSession()).data.session // const session = (await supabase.auth.getSession()).data.session
const { data, error } = await supabase.auth.getSession(); const { data, error } = await supabase.auth.getSession();
if (error) { if (error) {

View File

@ -2,9 +2,10 @@ import { Brand } from "@/components/ui/brand"
import { Input } from "@/components/ui/input" import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label" import { Label } from "@/components/ui/label"
import { SubmitButton } from "@/components/ui/submit-button" 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 { 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 { get } from "@vercel/edge-config"
import { Metadata } from "next" import { Metadata } from "next"
import { cookies, headers } from "next/headers" import { cookies, headers } from "next/headers"
@ -30,18 +31,20 @@ export default async function Login({
const localeString = locale; const localeString = locale;
const { t, resources } = await initTranslations(localeString, ['translation']); const { t, resources } = await initTranslations(localeString, ['translation']);
const supabase = createServerClient<Database>( // const supabase = createServerClient<Database>(
getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000", // getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000",
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, // process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{ // {
cookies: { // cookies: {
get(name: string) { // get(name: string) {
return cookieStore.get(name)?.value // return cookieStore.get(name)?.value
} // }
} // }
} // }
) // )
const supabase = getSupabaseServerClient()
const session = (await supabase.auth.getSession()).data.session const session = (await supabase.auth.getSession()).data.session
console.log("[login page]Login session:", session) console.log("[login page]Login session:", session)

View File

@ -10,23 +10,26 @@
let _env: Record<string, string> | null = null let _env: Record<string, string> | null = null
export function getRuntimeEnv(key: string): string | undefined { export function getRuntimeEnv(key: string): string | undefined {
// 打印缓存的 _env 变量和每次调用时的 key console.log("Checking env for key:", key)
console.log("Checking env for key:", key);
if (typeof window !== "undefined") {
// 如果 _env 存在,打印出缓存的 _env 并返回对应的值 if (!_env && typeof window.RUNTIME_ENV !== "undefined") {
if (_env) { _env = window.RUNTIME_ENV
console.log("Returning cached _env:", _env); console.log("Updated _env from window.RUNTIME_ENV:", _env)
return _env[key] }
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 获取的值 // 服务端始终动态读取 process.env
if (typeof window !== "undefined" && typeof window.RUNTIME_ENV !== "undefined") { const val = process.env[key]
_env = window.RUNTIME_ENV // 更新 _env 为最新的环境变量 console.log("Falling back to process.env for key:", key, "value:", val)
console.log("Updated _env from window.RUNTIME_ENV:", _env); return val
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]
} }

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 { createServerClient, type CookieOptions } from "@supabase/ssr"
import { cookies } from "next/headers" 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>) => { export function getSupabaseServerClient() {
return createServerClient( const supabaseUrl = getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000"
getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000", const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{ const cookieStore = cookies()
cookies: {
get(name: string) { return createServerClient<Database>(supabaseUrl, supabaseAnonKey, {
return cookieStore.get(name)?.value cookies: {
}, get(name: string) {
set(name: string, value: string, options: CookieOptions) { return cookieStore.get(name)?.value
try { },
cookieStore.set({ name, value, ...options }) set(name: string, value: string, options: CookieOptions) {
} catch (error) { try {
// The `set` method was called from a Server Component. cookieStore.set({ name, value, ...options })
// This can be ignored if you have middleware refreshing } catch (_) {}
// user sessions. },
} remove(name: string, options: CookieOptions) {
}, try {
remove(name: string, options: CookieOptions) { cookieStore.set({ name, value: "", ...options })
try { } catch (_) {}
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.
}
}
} }
} }
) })
} }