176 lines
6.0 KiB
Dart
176 lines
6.0 KiB
Dart
import 'package:firebase_auth/firebase_auth.dart';
|
|
import 'session_service.dart';
|
|
|
|
/// Service for handling Firebase Authentication
|
|
class AuthService {
|
|
static final FirebaseAuth _auth = FirebaseAuth.instance;
|
|
|
|
/// Get current user
|
|
static User? get currentUser {
|
|
return _auth.currentUser;
|
|
}
|
|
|
|
/// Get auth state changes stream
|
|
static Stream<User?> get authStateChanges {
|
|
return _auth.authStateChanges();
|
|
}
|
|
|
|
/// Sign up with email and password
|
|
static Future<UserCredential?> signUpWithEmailAndPassword({
|
|
required String email,
|
|
required String password,
|
|
String? displayName,
|
|
String? role,
|
|
}) async {
|
|
try {
|
|
print('DEBUG: Tentando criar conta para email: $email');
|
|
print('DEBUG: Password length: ${password.length}');
|
|
|
|
UserCredential result = await _auth.createUserWithEmailAndPassword(
|
|
email: email,
|
|
password: password,
|
|
);
|
|
|
|
print('DEBUG: Conta criada com sucesso para: ${result.user?.email}');
|
|
print('DEBUG: User ID: ${result.user?.uid}');
|
|
print('DEBUG: Email verified: ${result.user?.emailVerified}');
|
|
|
|
// Update user profile with display name
|
|
if (displayName != null && displayName.isNotEmpty) {
|
|
await result.user?.updateDisplayName(displayName);
|
|
print('DEBUG: Display name atualizado para: $displayName');
|
|
}
|
|
|
|
// Store user role in custom claims or Firestore (simplified for now)
|
|
if (role != null) {
|
|
print('DEBUG: Papel do usuário: $role');
|
|
// TODO: Store role in Firestore for future use
|
|
}
|
|
|
|
// Verificar se o email foi verificado
|
|
if (result.user != null && !result.user!.emailVerified) {
|
|
print('DEBUG: Email não verificado, tentando enviar verificação...');
|
|
await result.user!.sendEmailVerification();
|
|
print('DEBUG: Email de verificação enviado');
|
|
}
|
|
|
|
return result;
|
|
} on FirebaseAuthException catch (e) {
|
|
print('DEBUG: Erro Firebase ao criar conta: ${e.code} - ${e.message}');
|
|
String errorMessage = _getErrorMessage(e.code);
|
|
print('DEBUG: Mensagem de erro: $errorMessage');
|
|
throw Exception(errorMessage);
|
|
} catch (e) {
|
|
print('DEBUG: Erro genérico ao criar conta: $e');
|
|
throw Exception('Ocorreu um problema. Tente novamente');
|
|
}
|
|
}
|
|
|
|
/// Sign in with email and password
|
|
static Future<UserCredential?> signInWithEmailAndPassword({
|
|
required String email,
|
|
required String password,
|
|
}) async {
|
|
try {
|
|
print('DEBUG: Tentando login para email: $email');
|
|
print('DEBUG: Password length: ${password.length}');
|
|
|
|
// Verificar se há usuário atual
|
|
User? currentUser = _auth.currentUser;
|
|
print('DEBUG: Usuário atual: ${currentUser?.email}');
|
|
|
|
UserCredential result = await _auth.signInWithEmailAndPassword(
|
|
email: email,
|
|
password: password,
|
|
);
|
|
|
|
print('DEBUG: Login realizado com sucesso para: ${result.user?.email}');
|
|
print('DEBUG: User ID: ${result.user?.uid}');
|
|
print('DEBUG: Email verified: ${result.user?.emailVerified}');
|
|
print('DEBUG: Display name: ${result.user?.displayName}');
|
|
|
|
// Reload user data to ensure we have the latest information
|
|
await result.user?.reload();
|
|
print('DEBUG: User data reloaded');
|
|
print('DEBUG: Display name after reload: ${result.user?.displayName}');
|
|
|
|
return result;
|
|
} on FirebaseAuthException catch (e) {
|
|
print('DEBUG: Erro Firebase ao fazer login: ${e.code} - ${e.message}');
|
|
String errorMessage = _getErrorMessage(e.code);
|
|
print('DEBUG: Mensagem de erro: $errorMessage');
|
|
throw Exception(errorMessage);
|
|
} catch (e) {
|
|
print('DEBUG: Erro genérico ao fazer login: $e');
|
|
throw Exception('Ocorreu um problema. Tente novamente');
|
|
}
|
|
}
|
|
|
|
/// Sign out
|
|
static Future<void> signOut() async {
|
|
try {
|
|
print('DEBUG: Tentando fazer logout');
|
|
await _auth.signOut();
|
|
|
|
// Clear saved session
|
|
await SessionService.clearSession();
|
|
|
|
print('DEBUG: Logout e limpeza de sessão realizados com sucesso');
|
|
} catch (e) {
|
|
print('DEBUG: Erro ao fazer logout: $e');
|
|
}
|
|
}
|
|
|
|
/// Attempt auto-login with saved credentials
|
|
static Future<bool> attemptAutoLogin() async {
|
|
try {
|
|
final sessionData = await SessionService.shouldAutoLogin();
|
|
|
|
if (sessionData['shouldAutoLogin'] == true) {
|
|
final email = sessionData['email'];
|
|
print('DEBUG: Attempting auto-login for: $email');
|
|
|
|
// Note: For security reasons, we cannot auto-login without password
|
|
// This method just checks if auto-login is available
|
|
// The actual login still requires user to enter password
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
} catch (e) {
|
|
print('DEBUG: Error in auto-login attempt: $e');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// Get user-friendly error message
|
|
static String _getErrorMessage(String code) {
|
|
print('DEBUG: Processando código de erro: $code');
|
|
|
|
switch (code) {
|
|
case 'weak-password':
|
|
return 'A palavra-passe é muito fraca. Use pelo menos 8 caracteres.';
|
|
case 'invalid-email':
|
|
return 'O email fornecido é inválido. Verifique o formato.';
|
|
case 'user-disabled':
|
|
return 'Esta conta foi desativada. Contacte o suporte.';
|
|
case 'user-not-found':
|
|
return 'Nenhum utilizador encontrado com este email. Verifique os dados.';
|
|
case 'wrong-password':
|
|
return 'Palavra-passe incorreta. Tente novamente.';
|
|
case 'email-already-in-use':
|
|
return 'O email inserido já se encontra registrado';
|
|
case 'operation-not-allowed':
|
|
return 'Operação não permitida. Tente novamente.';
|
|
case 'invalid-credential':
|
|
return 'Credenciais inválidos. Verifique email e palavra-passe.';
|
|
case 'too-many-requests':
|
|
return 'Muitas tentativas. Aguarde alguns minutos antes de tentar novamente.';
|
|
case 'network-request-failed':
|
|
return 'Falha de conexão. Verifique sua internet e tente novamente.';
|
|
default:
|
|
return 'Ocorreu um problema. Tente novamente';
|
|
}
|
|
}
|
|
}
|