Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 1 addition & 23 deletions components/SupabaseProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function SupabaseProvider({ children }: { children: React.ReactNo

// Set up a shorter global timeout to prevent infinite loading
const globalTimeout = setTimeout(() => {
if (isMounted) {
if (isMounted && loading) {
errorLogger.warning(ErrorCategory.GENERAL, 'Auth initialization timeout - setting loading to false');
setLoading(false);
setUser(null);
Expand All @@ -47,12 +47,6 @@ export default function SupabaseProvider({ children }: { children: React.ReactNo
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;

console.log('🔧 Supabase Environment Check:', {
url: supabaseUrl ? 'Set' : 'Missing',
key: supabaseAnonKey ? 'Set' : 'Missing',
urlValue: supabaseUrl,
});

if (
!supabaseUrl ||
!supabaseAnonKey ||
Expand All @@ -61,7 +55,6 @@ export default function SupabaseProvider({ children }: { children: React.ReactNo
supabaseUrl.includes('placeholder') ||
supabaseAnonKey.includes('placeholder')
) {
console.error('❌ Supabase environment variables are missing or using placeholders');
errorLogger.warning(
ErrorCategory.GENERAL,
'Supabase environment variables are missing or using placeholders. Authentication will be disabled.'
Expand All @@ -75,8 +68,6 @@ export default function SupabaseProvider({ children }: { children: React.ReactNo
return;
}

console.log('✅ Supabase environment variables configured');

try {
// Create Supabase client
const client = createBrowserClient(supabaseUrl, supabaseAnonKey, {
Expand All @@ -95,13 +86,10 @@ export default function SupabaseProvider({ children }: { children: React.ReactNo

setSupabase(client);
setIsConfigured(true);
console.log('✅ Supabase client created successfully');

// Get initial session with shorter timeout
const getInitialSession = async () => {
try {
console.log('🔍 Getting initial session...');

// Create a shorter timeout promise
const timeoutPromise = new Promise<never>((_, reject) =>
setTimeout(() => reject(new Error('Session check timeout')), 3000) // Reduced to 3 seconds
Expand All @@ -113,17 +101,13 @@ export default function SupabaseProvider({ children }: { children: React.ReactNo
const result = await Promise.race([sessionPromise, timeoutPromise]);

if (result.error) {
console.error('❌ Error getting initial session:', result.error);
errorLogger.error(ErrorCategory.GENERAL, 'Error getting initial session:', result.error);
} else {
console.log('✅ Initial session check completed', result.data?.session?.user ? 'User found' : 'No user');
}

if (isMounted) {
setUser(result.data?.session?.user ?? null);
}
} catch (error) {
console.error('❌ Session check failed, continuing without auth:', error);
errorLogger.error(ErrorCategory.GENERAL, 'Error getting session (falling back to no auth):', error);
// On error, assume no user and continue
if (isMounted) {
Expand All @@ -132,7 +116,6 @@ export default function SupabaseProvider({ children }: { children: React.ReactNo
} finally {
if (isMounted) {
setLoading(false);
console.log('✅ Auth loading completed');
}
clearTimeout(globalTimeout);
}
Expand All @@ -144,7 +127,6 @@ export default function SupabaseProvider({ children }: { children: React.ReactNo
const {
data: { subscription },
} = client.auth.onAuthStateChange(async (event, session) => {
console.log('🔄 Auth state changed:', event, session?.user?.email);
errorLogger.info(ErrorCategory.GENERAL, `Auth state changed: ${event} ${session?.user?.email || 'no user'}`);

if (!isMounted) return;
Expand All @@ -155,11 +137,9 @@ export default function SupabaseProvider({ children }: { children: React.ReactNo
// Handle different auth events
if (event === 'SIGNED_OUT') {
// Clear any cached data - but don't force redirect
console.log('👋 User signed out');
errorLogger.info(ErrorCategory.GENERAL, 'User signed out');
} else if (event === 'SIGNED_IN' && session?.user) {
// Handle successful sign in
console.log('👤 User signed in:', session.user.email);
errorLogger.info(ErrorCategory.GENERAL, `User signed in: ${session.user.email}`);

// Try to sync user to database in background (non-blocking)
Expand Down Expand Up @@ -189,7 +169,6 @@ export default function SupabaseProvider({ children }: { children: React.ReactNo
isMounted = false;
};
} catch (error) {
console.error('❌ Error initializing Supabase:', error);
errorLogger.error(ErrorCategory.GENERAL, 'Error initializing Supabase', error);
if (isMounted) {
setIsConfigured(false);
Expand All @@ -209,7 +188,6 @@ export default function SupabaseProvider({ children }: { children: React.ReactNo
useEffect(() => {
const emergencyTimeout = setTimeout(() => {
if (loading) {
console.warn('⚠️ Emergency timeout: forcing loading to false');
setLoading(false);
}
}, 8000);
Expand Down