131 lines
3.7 KiB
Dart
131 lines
3.7 KiB
Dart
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
import '../constants/app_constants.dart';
|
|
|
|
class SupabaseService {
|
|
static final SupabaseClient _supabase = Supabase.instance.client;
|
|
|
|
// Initialize Supabase
|
|
static Future<void> initialize() async {
|
|
try {
|
|
print('DEBUG: Inicializando Supabase...');
|
|
print('DEBUG: URL: ${AppConstants.supabaseUrl}');
|
|
print(
|
|
'DEBUG: AnonKey: ${AppConstants.supabaseAnonKey.substring(0, 10)}...',
|
|
);
|
|
|
|
await Supabase.initialize(
|
|
url: AppConstants.supabaseUrl,
|
|
anonKey: AppConstants.supabaseAnonKey,
|
|
);
|
|
|
|
print('DEBUG: Supabase inicializado com sucesso!');
|
|
|
|
// Test connection
|
|
final currentUser = _supabase.auth.currentUser;
|
|
print('DEBUG: Usuário atual: ${currentUser?.email ?? 'null'}');
|
|
} catch (e) {
|
|
print('DEBUG: Erro ao inicializar Supabase: $e');
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
// Get current user
|
|
static User? get currentUser => _supabase.auth.currentUser;
|
|
|
|
// Sign up with email and password
|
|
static Future<AuthResponse> signUp({
|
|
required String email,
|
|
required String password,
|
|
required String name,
|
|
}) async {
|
|
try {
|
|
print('DEBUG: Criando conta - Email: $email, Name: $name');
|
|
|
|
final response = await _supabase.auth.signUp(
|
|
email: email,
|
|
password: password,
|
|
data: {'name': name},
|
|
);
|
|
|
|
print('DEBUG: Conta criada! User ID: ${response.user?.id}');
|
|
|
|
// Check if user was created successfully
|
|
if (response.user != null) {
|
|
print('DEBUG: Usuário criado com sucesso!');
|
|
return response;
|
|
} else {
|
|
print('DEBUG: Falha ao criar usuário - response.user é null');
|
|
throw Exception('Falha ao criar usuário. Tente novamente.');
|
|
}
|
|
} catch (e) {
|
|
print('DEBUG: Erro no signUp: $e');
|
|
throw Exception('Erro ao criar conta: $e');
|
|
}
|
|
}
|
|
|
|
// Sign in with email and password
|
|
static Future<AuthResponse> signIn({
|
|
required String email,
|
|
required String password,
|
|
}) async {
|
|
try {
|
|
print('DEBUG: Fazendo login - Email: $email');
|
|
|
|
final response = await _supabase.auth.signInWithPassword(
|
|
email: email,
|
|
password: password,
|
|
);
|
|
|
|
print('DEBUG: Login realizado! User ID: ${response.user?.id}');
|
|
return response;
|
|
} catch (e) {
|
|
print('DEBUG: Erro no signIn: $e');
|
|
throw Exception('Erro ao fazer login: $e');
|
|
}
|
|
}
|
|
|
|
// Sign out
|
|
static Future<void> signOut() async {
|
|
try {
|
|
await _supabase.auth.signOut();
|
|
print('DEBUG: Logout realizado');
|
|
} catch (e) {
|
|
print('DEBUG: Erro no signOut: $e');
|
|
throw Exception('Erro ao sair: $e');
|
|
}
|
|
}
|
|
|
|
// Reset password
|
|
static Future<void> resetPassword(String email) async {
|
|
try {
|
|
await _supabase.auth.resetPasswordForEmail(email);
|
|
print('DEBUG: Email de reset enviado para: $email');
|
|
} catch (e) {
|
|
print('DEBUG: Erro no resetPassword: $e');
|
|
throw Exception('Erro ao redefinir senha: $e');
|
|
}
|
|
}
|
|
|
|
// Test connection to Supabase
|
|
static Future<bool> testConnection() async {
|
|
try {
|
|
print('DEBUG: Testando conexão com Supabase...');
|
|
|
|
// Test with auth service instead of database
|
|
final session = _supabase.auth.currentSession;
|
|
print('DEBUG: Sessão atual: ${session != null ? 'ativa' : 'null'}');
|
|
|
|
// Try to get auth settings (this should work even without tables)
|
|
print('DEBUG: Conexão básica funcionando!');
|
|
return true;
|
|
} catch (e) {
|
|
print('DEBUG: Erro na conexão: $e');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Listen to auth state changes
|
|
static Stream<AuthState> get authStateChanges =>
|
|
_supabase.auth.onAuthStateChange;
|
|
}
|