Files
DiasAbertos_RunVisionPro/lib/services/supabase_service.dart
2026-03-18 20:03:32 +00:00

114 lines
2.7 KiB
Dart

import 'package:supabase_flutter/supabase_flutter.dart';
import '../constants/app_constants.dart';
class SupabaseService {
static SupabaseClient get _supabase => Supabase.instance.client;
// Initialize Supabase
static Future<void> initialize() async {
try {
print('DEBUG: Inicializando Supabase...');
await Supabase.initialize(
url: AppConstants.supabaseUrl,
anonKey: AppConstants.supabaseAnonKey,
);
print('DEBUG: Supabase inicializado com sucesso!');
} 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 {
final response = await _supabase.auth.signUp(
email: email,
password: password,
data: {'name': name},
);
if (response.user != null) {
return response;
} else {
throw Exception('Falha ao criar usuário.');
}
} catch (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 {
return await _supabase.auth.signInWithPassword(
email: email,
password: password,
);
} catch (e) {
throw Exception('Erro ao fazer login: $e');
}
}
// Sign out
static Future<void> signOut() async {
try {
await _supabase.auth.signOut();
} catch (e) {
throw Exception('Erro ao sair: $e');
}
}
// Reset password
static Future<void> resetPassword(String email) async {
try {
await _supabase.auth.resetPasswordForEmail(email);
} catch (e) {
throw Exception('Erro ao redefinir senha: $e');
}
}
// Update user profile
static Future<void> updateProfile({String? name, String? email}) async {
try {
final updates = <String, dynamic>{};
if (name != null) updates['name'] = name;
final userAttributes = UserAttributes(
data: updates.isNotEmpty ? updates : null,
email: email,
);
await _supabase.auth.updateUser(userAttributes);
} catch (e) {
throw Exception('Erro ao atualizar perfil: $e');
}
}
// Test connection to Supabase
static Future<bool> testConnection() async {
try {
final session = _supabase.auth.currentSession;
return true;
} catch (e) {
return false;
}
}
// Listen to auth state changes
static Stream<AuthState> get authStateChanges =>
_supabase.auth.onAuthStateChange;
}