Initial commit
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import { createClient, SupabaseClient } from '@supabase/supabase-js';
|
||||
import { logger } from './debugConfig';
|
||||
|
||||
const appProtocol = import.meta.env.VITE_APP_PROTOCOL;
|
||||
const supabaseUrl = `${appProtocol}://${import.meta.env.VITE_SUPABASE_URL}`;
|
||||
const supabaseAnonKey = import.meta.env.VITE_SUPABASE_ANON_KEY;
|
||||
|
||||
logger.info('supabase-client', '🔄 Supabase configuration', {
|
||||
url: supabaseUrl,
|
||||
key: supabaseAnonKey,
|
||||
});
|
||||
|
||||
if (!supabaseUrl || !supabaseAnonKey) {
|
||||
logger.error('supabase-client', '❌ Missing Supabase configuration', {
|
||||
hasUrl: !!supabaseUrl,
|
||||
hasKey: !!supabaseAnonKey,
|
||||
});
|
||||
throw new Error('Missing Supabase configuration');
|
||||
}
|
||||
|
||||
// Lazy-loaded Supabase client instance for non-auth operations
|
||||
let supabaseInstance: SupabaseClient | null = null;
|
||||
|
||||
const getSupabaseClient = () => {
|
||||
if (!supabaseInstance) {
|
||||
logger.info('supabase-client', '🔄 Initializing Supabase client');
|
||||
supabaseInstance = createClient(
|
||||
supabaseUrl,
|
||||
supabaseAnonKey,
|
||||
{
|
||||
auth: {
|
||||
flowType: 'pkce',
|
||||
autoRefreshToken: true,
|
||||
persistSession: true,
|
||||
detectSessionInUrl: false,
|
||||
storage: window.localStorage,
|
||||
storageKey: 'supabase.auth.token',
|
||||
debug: true
|
||||
},
|
||||
global: {
|
||||
headers: {
|
||||
'X-Client-Info': 'classroom-copilot',
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
// Log configuration in development
|
||||
logger.info('supabase-client', '🔄 Supabase client configuration loaded', {
|
||||
url: supabaseUrl,
|
||||
hasKey: !!supabaseAnonKey,
|
||||
storageKey: 'supabase.auth.token'
|
||||
});
|
||||
}
|
||||
return supabaseInstance;
|
||||
};
|
||||
|
||||
// Export a proxy that will lazy load the client when needed
|
||||
export const supabase = new Proxy({} as SupabaseClient, {
|
||||
get: (target, prop) => {
|
||||
const client = getSupabaseClient();
|
||||
return client[prop as keyof SupabaseClient];
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user