fix env check at wrong time
This commit is contained in:
@@ -1,17 +1,32 @@
|
||||
import { createClient } from '@supabase/supabase-js'
|
||||
import type { Database } from './types'
|
||||
|
||||
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
|
||||
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
|
||||
let _supabase: ReturnType<typeof createClient<Database>> | null = null
|
||||
|
||||
if (!supabaseUrl || !supabaseAnonKey) {
|
||||
throw new Error(
|
||||
'Missing Supabase environment variables. Add NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY to your .env.local file.'
|
||||
)
|
||||
function getSupabase() {
|
||||
if (!_supabase) {
|
||||
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
|
||||
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
|
||||
|
||||
if (!supabaseUrl || !supabaseAnonKey) {
|
||||
throw new Error(
|
||||
'Missing required environment variables: NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY. Please configure them in your deployment settings or .env.local file.'
|
||||
)
|
||||
}
|
||||
|
||||
_supabase = createClient<Database>(supabaseUrl, supabaseAnonKey)
|
||||
}
|
||||
return _supabase
|
||||
}
|
||||
|
||||
/**
|
||||
* Supabase client for client-side use with anon key
|
||||
* Uses Row Level Security (RLS) policies
|
||||
*
|
||||
* Initialized lazily to avoid requiring env vars at build time
|
||||
*/
|
||||
export const supabase = createClient<Database>(supabaseUrl, supabaseAnonKey)
|
||||
export const supabase = new Proxy({} as ReturnType<typeof createClient<Database>>, {
|
||||
get(_target, prop) {
|
||||
return Reflect.get(getSupabase(), prop)
|
||||
},
|
||||
})
|
||||
|
||||
+25
-10
@@ -1,23 +1,38 @@
|
||||
import { createClient } from '@supabase/supabase-js'
|
||||
import type { Database } from './types'
|
||||
|
||||
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
|
||||
const supabaseServiceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY
|
||||
let _supabaseAdmin: ReturnType<typeof createClient<Database>> | null = null
|
||||
|
||||
if (!supabaseUrl || !supabaseServiceRoleKey) {
|
||||
throw new Error(
|
||||
'Missing Supabase environment variables. Add NEXT_PUBLIC_SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY to your .env.local file.'
|
||||
)
|
||||
function getSupabaseAdmin() {
|
||||
if (!_supabaseAdmin) {
|
||||
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
|
||||
const supabaseServiceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY
|
||||
|
||||
if (!supabaseUrl || !supabaseServiceRoleKey) {
|
||||
throw new Error(
|
||||
'Missing required environment variables: NEXT_PUBLIC_SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY. Please configure them in your deployment settings or .env.local file.'
|
||||
)
|
||||
}
|
||||
|
||||
_supabaseAdmin = createClient<Database>(supabaseUrl, supabaseServiceRoleKey, {
|
||||
auth: {
|
||||
persistSession: false,
|
||||
autoRefreshToken: false,
|
||||
},
|
||||
})
|
||||
}
|
||||
return _supabaseAdmin
|
||||
}
|
||||
|
||||
/**
|
||||
* Supabase client for server-side use with service role key
|
||||
* Bypasses Row Level Security (RLS) - use with caution
|
||||
* Always filter by user_id to enforce permissions
|
||||
*
|
||||
* Initialized lazily to avoid requiring env vars at build time
|
||||
*/
|
||||
export const supabaseAdmin = createClient<Database>(supabaseUrl, supabaseServiceRoleKey, {
|
||||
auth: {
|
||||
persistSession: false,
|
||||
autoRefreshToken: false,
|
||||
export const supabaseAdmin = new Proxy({} as ReturnType<typeof createClient<Database>>, {
|
||||
get(_target, prop) {
|
||||
return Reflect.get(getSupabaseAdmin(), prop)
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user