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