Files
LearnIT/lib/core/services/auth_service.dart
2026-05-08 11:53:18 +01:00

156 lines
5.2 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,
}) 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}');
// 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}');
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';
}
}
}