refactor: simplify UserContext and supabaseClient
- Refactored UserContext.tsx (67 lines simplified) - Simplified supabaseClient.ts (82 lines reduced) - Updated .env.development for local dev
This commit is contained in:
+26
-56
@@ -4,65 +4,35 @@ import { logger } from './debugConfig';
|
||||
const supabaseUrl = 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',
|
||||
},
|
||||
},
|
||||
// Allow JWT issuer mismatch for local development
|
||||
db: {
|
||||
schema: 'public'
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// 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];
|
||||
}
|
||||
logger.info('supabase-client', '🔄 Initializing Supabase client', {
|
||||
url: supabaseUrl,
|
||||
hasKey: !!supabaseAnonKey,
|
||||
});
|
||||
|
||||
export const supabase: SupabaseClient = 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',
|
||||
},
|
||||
},
|
||||
db: {
|
||||
schema: 'public'
|
||||
}
|
||||
}
|
||||
);
|
||||
Reference in New Issue
Block a user