first commit
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
import { createClient } from '@supabase/supabase-js';
|
||||
import type { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
const supabaseUrl = import.meta.env.VITE_SUPABASE_URL || '';
|
||||
const supabaseAnonKey = import.meta.env.VITE_SUPABASE_ANON_KEY || '';
|
||||
|
||||
// Check if Supabase is configured
|
||||
export const isSupabaseConfigured = !!(supabaseUrl && supabaseAnonKey);
|
||||
|
||||
// Create a mock Supabase client for offline mode
|
||||
const createMockClient = (): SupabaseClient => {
|
||||
const mockClient = {
|
||||
auth: {
|
||||
getSession: async () => ({ data: { session: null }, error: null }),
|
||||
onAuthStateChange: () => ({ data: { subscription: { unsubscribe: () => {} } } }),
|
||||
signInWithOAuth: async () => ({ data: null, error: { message: 'Supabase not configured' } }),
|
||||
signInWithOtp: async () => ({ data: null, error: { message: 'Supabase not configured' } }),
|
||||
verifyOtp: async () => ({ data: null, error: { message: 'Supabase not configured' } }),
|
||||
signUp: async () => ({ data: null, error: { message: 'Supabase not configured' } }),
|
||||
signInWithPassword: async () => ({ data: null, error: { message: 'Supabase not configured' } }),
|
||||
signOut: async () => ({ error: null })
|
||||
},
|
||||
from: () => ({
|
||||
select: () => ({
|
||||
order: () => ({ limit: async () => ({ data: [], error: null }) }),
|
||||
insert: () => ({ select: () => ({ single: async () => ({ data: null, error: { message: 'Supabase not configured' } }) }) })
|
||||
}),
|
||||
on: () => ({ subscribe: () => ({ unsubscribe: () => {} }) }),
|
||||
insert: () => ({ select: () => ({ single: async () => ({ data: null, error: { message: 'Supabase not configured' } }) }) })
|
||||
}),
|
||||
channel: () => ({
|
||||
on: () => ({
|
||||
subscribe: () => ({ unsubscribe: () => {} })
|
||||
})
|
||||
}),
|
||||
removeChannel: () => {}
|
||||
} as unknown as SupabaseClient;
|
||||
|
||||
return mockClient;
|
||||
};
|
||||
|
||||
// Create real or mock client based on configuration
|
||||
export const supabase = isSupabaseConfigured
|
||||
? createClient(supabaseUrl, supabaseAnonKey, {
|
||||
auth: {
|
||||
persistSession: true,
|
||||
autoRefreshToken: true,
|
||||
detectSessionInUrl: true
|
||||
},
|
||||
global: {
|
||||
headers: {
|
||||
'apikey': supabaseAnonKey
|
||||
}
|
||||
}
|
||||
})
|
||||
: createMockClient();
|
||||
|
||||
if (!isSupabaseConfigured) {
|
||||
console.warn('⚠️ Supabase environment variables are not set. Running in offline/mock mode.');
|
||||
} else {
|
||||
// Test connection to Supabase (runs once on module load, but doesn't block)
|
||||
// Use setTimeout to avoid blocking module initialization
|
||||
setTimeout(() => {
|
||||
testSupabaseConnection();
|
||||
}, 100);
|
||||
}
|
||||
|
||||
// Test Supabase connection without needing tables
|
||||
async function testSupabaseConnection() {
|
||||
if (!isSupabaseConfigured) return;
|
||||
|
||||
try {
|
||||
// Simple health check: try to get current session
|
||||
// This doesn't require any tables, just checks if we can communicate with Supabase
|
||||
const { data, error } = await supabase.auth.getSession();
|
||||
|
||||
if (error) {
|
||||
console.error('❌ خطا در اتصال به Supabase:', error.message);
|
||||
console.log('📋 اطلاعات اتصال:');
|
||||
console.log(' URL:', supabaseUrl);
|
||||
console.log(' Key:', supabaseAnonKey ? `${supabaseAnonKey.substring(0, 20)}...` : 'not set');
|
||||
console.log('💡 نکته: اگر URL شما custom domain است، مطمئن شوید که به درستی پیکربندی شده است.');
|
||||
} else {
|
||||
console.log('✅ اتصال به Supabase با موفقیت برقرار شد!');
|
||||
console.log('📋 اطلاعات اتصال:');
|
||||
console.log(' URL:', supabaseUrl);
|
||||
console.log(' Session:', data.session ? 'فعال' : 'غیرفعال (کاربر لاگین نشده)');
|
||||
}
|
||||
} catch (err) {
|
||||
const errorMessage = err instanceof Error ? err.message : String(err);
|
||||
console.error('❌ خطا در تست اتصال Supabase:', errorMessage);
|
||||
console.log('📋 لطفاً بررسی کنید:');
|
||||
console.log(' 1. فایل .env درست تنظیم شده است؟');
|
||||
console.log(' 2. URL و Key صحیح هستند؟');
|
||||
console.log(' 3. اینترنت متصل است؟');
|
||||
console.log(' 4. اگر از custom domain استفاده میکنید، مطمئن شوید که CORS و تنظیمات درست است.');
|
||||
}
|
||||
}
|
||||
|
||||
// Database types (extend as needed)
|
||||
export type Profile = {
|
||||
id: string;
|
||||
email?: string;
|
||||
phone?: string;
|
||||
full_name?: string;
|
||||
avatar_url?: string;
|
||||
created_at?: string;
|
||||
};
|
||||
|
||||
export type Request = {
|
||||
id: string;
|
||||
user_id: string;
|
||||
title: string;
|
||||
description: string;
|
||||
status: 'pending' | 'in_progress' | 'completed';
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
};
|
||||
|
||||
export type Message = {
|
||||
id: string;
|
||||
user_id: string;
|
||||
content: string;
|
||||
created_at: string;
|
||||
user?: {
|
||||
email?: string;
|
||||
phone?: string;
|
||||
full_name?: string;
|
||||
avatar_url?: string;
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user