100 lines
3.3 KiB
TypeScript
100 lines
3.3 KiB
TypeScript
import { createClient } from '@supabase/supabase-js';
|
|
|
|
// Get Supabase configuration from environment variables
|
|
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL || process.env.SUPABASE_URL || '';
|
|
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY || process.env.SUPABASE_ANON_KEY || '';
|
|
|
|
console.log('Supabase Configuration Check:', {
|
|
urlDefined: !!supabaseUrl,
|
|
keyDefined: !!supabaseAnonKey,
|
|
url: supabaseUrl,
|
|
// Print partial key for debugging
|
|
keyPrefix: supabaseAnonKey ? supabaseAnonKey.substring(0, 20) + '...' : 'undefined',
|
|
keyLength: supabaseAnonKey ? supabaseAnonKey.length : 0
|
|
});
|
|
|
|
if (!supabaseUrl || !supabaseAnonKey) {
|
|
console.error('Supabase URL and Anon Key are required');
|
|
}
|
|
|
|
// Try to decode JWT token and print decoded content
|
|
try {
|
|
if (supabaseAnonKey) {
|
|
const parts = supabaseAnonKey.split('.');
|
|
if (parts.length === 3) {
|
|
const payload = parts[1];
|
|
const decoded = atob(payload);
|
|
console.log('JWT Payload:', decoded);
|
|
} else {
|
|
console.error('Invalid JWT format, expected 3 parts but got:', parts.length);
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('JWT decoding failed:', error);
|
|
}
|
|
|
|
// Create custom cookie handling logic
|
|
const customStorage = {
|
|
getItem: (key: string): string | null => {
|
|
if (typeof document === 'undefined') return null;
|
|
const cookie = document.cookie
|
|
.split(';')
|
|
.find((c) => c.trim().startsWith(`${key}=`));
|
|
return cookie ? cookie.split('=')[1] : null;
|
|
},
|
|
setItem: (key: string, value: string): void => {
|
|
if (typeof document === 'undefined') return;
|
|
|
|
// Get current host and port to handle different ports on localhost
|
|
const host = typeof window !== 'undefined' ? window.location.hostname : '';
|
|
|
|
// Set cookie, using generic domain for localhost
|
|
document.cookie = `${key}=${value}; path=/; max-age=${60 * 60 * 24 * 7}; samesite=lax; domain=${host}`;
|
|
|
|
console.log(`Cookie ${key} has been set, domain=${host}`);
|
|
},
|
|
removeItem: (key: string): void => {
|
|
if (typeof document === 'undefined') return;
|
|
|
|
// Get current host and port to handle different ports on localhost
|
|
const host = typeof window !== 'undefined' ? window.location.hostname : '';
|
|
|
|
// Remove cookie, using generic domain for localhost
|
|
document.cookie = `${key}=; path=/; max-age=0; samesite=lax; domain=${host}`;
|
|
|
|
console.log(`Cookie ${key} has been removed`);
|
|
},
|
|
};
|
|
|
|
// Create Supabase client
|
|
export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
|
|
auth: {
|
|
persistSession: true,
|
|
autoRefreshToken: true,
|
|
detectSessionInUrl: true,
|
|
storageKey: 'sb-auth-token',
|
|
storage: customStorage
|
|
}
|
|
});
|
|
|
|
// Test Supabase connection
|
|
supabase.auth.onAuthStateChange((event, session) => {
|
|
console.log(`Supabase auth event: ${event}`, session ? 'Session exists' : 'No session');
|
|
if (session) {
|
|
console.log('Current user:', session.user.email);
|
|
}
|
|
});
|
|
|
|
// Try to perform health check
|
|
async function checkSupabaseHealth() {
|
|
try {
|
|
const { data, error } = await supabase.from('_health').select('*').limit(1);
|
|
console.log('Supabase health check:', error ? `Error: ${error.message}` : 'Success', data);
|
|
} catch (error) {
|
|
console.error('Supabase health check error:', error);
|
|
}
|
|
}
|
|
|
|
checkSupabaseHealth();
|
|
|
|
export default supabase;
|