-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabaseClient.ts
More file actions
62 lines (49 loc) · 2.7 KB
/
Copy pathsupabaseClient.ts
File metadata and controls
62 lines (49 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { createClient, SupabaseClient } from '@supabase/supabase-js';
import { Database } from './types/supabase';
// Function to create the actual client instance, encapsulates env checks.
const createActualSupabaseClient = (): SupabaseClient<Database> => {
const env = (import.meta as any).env;
// Log para verificar o objeto env completo
console.log("[SupabaseClient] import.meta.env:", env);
if (!env) {
console.error("CRITICAL ERROR: import.meta.env is undefined inside createActualSupabaseClient(). This should not happen if Vite is working correctly.");
throw new Error(
'As variáveis de ambiente do Vite (import.meta.env) não estão disponíveis. ' +
'Verifique a configuração do seu projeto Vite e o arquivo .env.'
);
}
const supabaseUrl = env.VITE_SUPABASE_URL;
const supabaseAnonKey = env.VITE_SUPABASE_ANON_KEY;
// Logs específicos para as chaves
console.log("[SupabaseClient] VITE_SUPABASE_URL from env:", supabaseUrl);
console.log("[SupabaseClient] VITE_SUPABASE_ANON_KEY from env:", supabaseAnonKey);
if (!supabaseUrl || !supabaseAnonKey) {
let errorMessage = 'A URL e/ou a Chave Anônima do Supabase estão ausentes nas variáveis de ambiente. ';
errorMessage += 'Certifique-se de que VITE_SUPABASE_URL e VITE_SUPABASE_ANON_KEY estão definidas no seu arquivo .env na raiz do projeto, ';
errorMessage += 'e que o servidor de desenvolvimento Vite foi reiniciado após quaisquer alterações no arquivo .env.';
if (!env.VITE_SUPABASE_URL) {
errorMessage += ' A variável VITE_SUPABASE_URL está faltando.';
console.error("Supabase Env: VITE_SUPABASE_URL is missing from env object:", env);
}
if (!env.VITE_SUPABASE_ANON_KEY) {
errorMessage += ' A variável VITE_SUPABASE_ANON_KEY está faltando.';
console.error("Supabase Env: VITE_SUPABASE_ANON_KEY is missing from env object:", env);
}
throw new Error(errorMessage);
}
return createClient<Database>(supabaseUrl, supabaseAnonKey);
};
// Create and export the single instance directly.
// This runs when the module is first imported.
export const supabase: SupabaseClient<Database> = createActualSupabaseClient();
// Updated getSupabaseClient to return the already initialized instance.
// This maintains compatibility for any code still calling it as a function,
// though direct import of 'supabase' is now preferred.
export const getSupabaseClient = (): SupabaseClient<Database> => {
return supabase;
};
// Helper para obter o ID do usuário logado de forma segura, using the direct instance
export const getSupabaseUserId = async (): Promise<string | null> => {
const { data: { session } } = await supabase.auth.getSession();
return session?.user?.id || null;
};