mirror of
https://github.com/marcogll/AnchorOS.git
synced 2026-03-15 14:24:27 +00:00
Fix Supabase connection issues with lazy initialization and enhanced logging
This commit is contained in:
@@ -1,14 +1,42 @@
|
||||
import { createClient } from '@supabase/supabase-js'
|
||||
|
||||
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL!
|
||||
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
|
||||
// Lazy initialization to ensure env vars are available at runtime
|
||||
let supabaseInstance: ReturnType<typeof createClient> | null = null
|
||||
|
||||
function getSupabaseClient() {
|
||||
if (!supabaseInstance) {
|
||||
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
|
||||
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
|
||||
|
||||
console.log('=== SUPABASE CLIENT INIT ===')
|
||||
console.log('SUPABASE_URL available:', !!supabaseUrl)
|
||||
console.log('SUPABASE_ANON_KEY available:', !!supabaseAnonKey)
|
||||
console.log('SUPABASE_URL value:', supabaseUrl)
|
||||
console.log('SUPABASE_ANON_KEY preview:', supabaseAnonKey ? supabaseAnonKey.substring(0, 20) + '...' : 'null')
|
||||
|
||||
if (!supabaseUrl || !supabaseAnonKey) {
|
||||
throw new Error(`Missing Supabase environment variables: URL=${!!supabaseUrl}, KEY=${!!supabaseAnonKey}`)
|
||||
}
|
||||
|
||||
supabaseInstance = createClient(supabaseUrl, supabaseAnonKey, {
|
||||
auth: {
|
||||
autoRefreshToken: true,
|
||||
persistSession: true,
|
||||
detectSessionInUrl: true
|
||||
}
|
||||
})
|
||||
|
||||
console.log('Supabase client initialized successfully')
|
||||
}
|
||||
|
||||
return supabaseInstance
|
||||
}
|
||||
|
||||
// Public Supabase client for client-side operations
|
||||
export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
|
||||
auth: {
|
||||
autoRefreshToken: true,
|
||||
persistSession: true,
|
||||
detectSessionInUrl: true
|
||||
export const supabase = new Proxy({} as ReturnType<typeof createClient>, {
|
||||
get(target, prop) {
|
||||
const client = getSupabaseClient()
|
||||
return client[prop as keyof typeof client]
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user