Telas de login e dashboard de estudante feito
This commit is contained in:
128
lib/core/services/auth_service.dart
Normal file
128
lib/core/services/auth_service.dart
Normal file
@@ -0,0 +1,128 @@
|
||||
import 'package:firebase_auth/firebase_auth.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();
|
||||
print('DEBUG: Logout realizado com sucesso');
|
||||
} catch (e) {
|
||||
print('DEBUG: Erro ao fazer logout: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// 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';
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user