Spaces:
Sleeping
Sleeping
| import { createClient, SupabaseClient } from '@supabase/supabase-js'; | |
| let supabaseInstance: SupabaseClient | null = null; | |
| export function initializeSupabase(url: string, key: string) { | |
| if (url && key) { | |
| supabaseInstance = createClient(url, key); | |
| } | |
| } | |
| // Proxy wrapper that delegates all calls to the dynamically initialized instance | |
| export const supabase = new Proxy({} as any, { | |
| get(_target, prop) { | |
| if (prop === 'auth' && localStorage.getItem('algospaced_sandbox_mode') === 'true') { | |
| const mockSession = { | |
| access_token: 'sandbox', | |
| user: { | |
| id: 'sandbox-user-id', | |
| email: 'sandbox@algospaced.io', | |
| email_confirmed_at: new Date().toISOString(), | |
| last_sign_in_at: new Date().toISOString(), | |
| role: 'authenticated', | |
| aud: 'authenticated', | |
| } | |
| }; | |
| return { | |
| getSession: async () => ({ data: { session: mockSession }, error: null }), | |
| getUser: async () => ({ data: { user: mockSession.user }, error: null }), | |
| onAuthStateChange: (callback: any) => { | |
| setTimeout(() => callback('SIGNED_IN', mockSession), 0); | |
| return { | |
| data: { | |
| subscription: { | |
| unsubscribe: () => {} | |
| } | |
| } | |
| }; | |
| }, | |
| signOut: async () => { | |
| localStorage.removeItem('algospaced_sandbox_mode'); | |
| window.location.reload(); | |
| return { error: null }; | |
| } | |
| }; | |
| } | |
| if (!supabaseInstance) { | |
| // Fallback: try loading from build-time environment if not initialized yet | |
| const url = import.meta.env.VITE_SUPABASE_URL || ''; | |
| const key = import.meta.env.VITE_SUPABASE_ANON_KEY || ''; | |
| if (url && key) { | |
| supabaseInstance = createClient(url, key); | |
| } else { | |
| throw new Error("Supabase client is not initialized. Please call initializeSupabase(url, key) first."); | |
| } | |
| } | |
| return (supabaseInstance as any)[prop]; | |
| } | |
| }) as unknown as SupabaseClient; | |