fix: make Supabase client lazy to avoid errors when credentials missing
This commit is contained in:
@@ -1,18 +1,22 @@
|
|||||||
import { createClient } from '@supabase/supabase-js';
|
import { createClient, SupabaseClient } from '@supabase/supabase-js';
|
||||||
|
|
||||||
const supabaseUrl = import.meta.env.VITE_SUPABASE_URL;
|
const supabaseUrl = import.meta.env.VITE_SUPABASE_URL;
|
||||||
const supabaseAnonKey = import.meta.env.VITE_SUPABASE_ANON_KEY;
|
const supabaseAnonKey = import.meta.env.VITE_SUPABASE_ANON_KEY;
|
||||||
|
|
||||||
|
// Lazy initialization - only create client when credentials are available
|
||||||
|
let _supabase: SupabaseClient | null = null;
|
||||||
|
|
||||||
|
const getSupabaseClient = (): SupabaseClient => {
|
||||||
if (!supabaseUrl || !supabaseAnonKey) {
|
if (!supabaseUrl || !supabaseAnonKey) {
|
||||||
throw new Error('Missing Supabase environment variables');
|
throw new Error('Missing Supabase environment variables. Please set VITE_SUPABASE_URL and VITE_SUPABASE_ANON_KEY.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create Supabase client with proper session management enabled
|
if (!_supabase) {
|
||||||
export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
|
_supabase = createClient(supabaseUrl, supabaseAnonKey, {
|
||||||
auth: {
|
auth: {
|
||||||
autoRefreshToken: true, // ✅ Auto-refresh expired tokens
|
autoRefreshToken: true,
|
||||||
persistSession: true, // ✅ Persist session in storage
|
persistSession: true,
|
||||||
detectSessionInUrl: true, // ✅ Auto-detect OAuth callback
|
detectSessionInUrl: true,
|
||||||
storage: typeof window !== 'undefined' ? window.localStorage : undefined,
|
storage: typeof window !== 'undefined' ? window.localStorage : undefined,
|
||||||
},
|
},
|
||||||
global: {
|
global: {
|
||||||
@@ -21,11 +25,26 @@ export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return _supabase;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Export a proxy that lazily initializes the client
|
||||||
|
export const supabase = new Proxy({} as SupabaseClient, {
|
||||||
|
get(_, prop) {
|
||||||
|
return getSupabaseClient()[prop as keyof SupabaseClient];
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
// Helper to create authenticated client (kept for backward compatibility)
|
// Helper to create authenticated client (kept for backward compatibility)
|
||||||
// NOTE: With proper session management, this should no longer be needed
|
// NOTE: With proper session management, this should no longer be needed
|
||||||
// Once session is set via supabase.auth.setSession(), the base client will have auth context
|
// Once session is set via supabase.auth.setSession(), the base client will have auth context
|
||||||
export const getAuthenticatedClient = (accessToken: string) => {
|
export const getAuthenticatedClient = (accessToken: string) => {
|
||||||
|
if (!supabaseUrl || !supabaseAnonKey) {
|
||||||
|
throw new Error('Missing Supabase environment variables. Please set VITE_SUPABASE_URL and VITE_SUPABASE_ANON_KEY.');
|
||||||
|
}
|
||||||
|
|
||||||
return createClient(supabaseUrl, supabaseAnonKey, {
|
return createClient(supabaseUrl, supabaseAnonKey, {
|
||||||
auth: {
|
auth: {
|
||||||
autoRefreshToken: false,
|
autoRefreshToken: false,
|
||||||
|
|||||||
Reference in New Issue
Block a user