65 lines
1.9 KiB
TypeScript
65 lines
1.9 KiB
TypeScript
import { createClient } from '@supabase/supabase-js';
|
|
|
|
// 从环境变量获取Supabase配置
|
|
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,
|
|
// 打印部分key以便调试
|
|
keyPrefix: supabaseAnonKey ? supabaseAnonKey.substring(0, 20) + '...' : 'undefined',
|
|
keyLength: supabaseAnonKey ? supabaseAnonKey.length : 0
|
|
});
|
|
|
|
if (!supabaseUrl || !supabaseAnonKey) {
|
|
console.error('Supabase URL and Anon Key are required');
|
|
}
|
|
|
|
// 尝试解码JWT token并打印解码内容
|
|
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解码失败:', error);
|
|
}
|
|
|
|
// 创建Supabase客户端
|
|
export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
|
|
auth: {
|
|
persistSession: true,
|
|
autoRefreshToken: true,
|
|
detectSessionInUrl: true,
|
|
}
|
|
});
|
|
|
|
// 测试Supabase连接
|
|
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);
|
|
}
|
|
});
|
|
|
|
// 尝试执行健康检查
|
|
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;
|