firebase esta feita acabar o register

This commit is contained in:
2026-01-06 17:17:37 +00:00
parent 24bd74aedc
commit 44937ca6d3
14 changed files with 594 additions and 233 deletions

View File

@@ -1,6 +1,9 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
class LoginController with ChangeNotifier {
final FirebaseAuth _auth = FirebaseAuth.instance;
final TextEditingController emailController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
@@ -20,26 +23,19 @@ class LoginController with ChangeNotifier {
}
String? validateEmail(String? value) {
if (value == null || value.isEmpty) {
return 'Por favor, insira o seu email';
}
if (value == null || value.isEmpty) return 'Por favor, insira o seu email';
final emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
if (!emailRegex.hasMatch(value)) {
return 'Por favor, insira um email válido';
}
if (!emailRegex.hasMatch(value)) return 'Por favor, insira um email válido';
return null;
}
String? validatePassword(String? value) {
if (value == null || value.isEmpty) {
return 'Por favor, insira a sua password';
}
if (value.length < 6) {
return 'A password deve ter pelo menos 6 caracteres';
}
if (value == null || value.isEmpty) return 'Por favor, insira a sua password';
if (value.length < 6) return 'A password deve ter pelo menos 6 caracteres';
return null;
}
// --- MÉTODO PARA ENTRAR (LOGIN) ---
Future<bool> login() async {
_emailError = validateEmail(emailController.text);
_passwordError = validatePassword(passwordController.text);
@@ -53,21 +49,76 @@ class LoginController with ChangeNotifier {
notifyListeners();
try {
await Future.delayed(const Duration(seconds: 2));
await _auth.signInWithEmailAndPassword(
email: emailController.text.trim(),
password: passwordController.text.trim(),
);
_isLoading = false;
notifyListeners();
return true;
} catch (e) {
} on FirebaseAuthException catch (e) {
_isLoading = false;
_emailError = 'Erro no login. Tente novamente.';
_handleFirebaseError(e.code);
notifyListeners();
return false;
}
}
// --- MÉTODO PARA CRIAR CONTA (SIGN UP) ---
Future<bool> signUp() async {
_emailError = validateEmail(emailController.text);
_passwordError = validatePassword(passwordController.text);
if (_emailError != null || _passwordError != null) {
notifyListeners();
return false;
}
_isLoading = true;
notifyListeners();
try {
await _auth.createUserWithEmailAndPassword(
email: emailController.text.trim(),
password: passwordController.text.trim(),
);
_isLoading = false;
notifyListeners();
return true;
} on FirebaseAuthException catch (e) {
_isLoading = false;
_handleFirebaseError(e.code);
notifyListeners();
return false;
}
}
void _handleFirebaseError(String code) {
switch (code) {
case 'email-already-in-use':
_emailError = 'Este e-mail já está a ser utilizado.';
break;
case 'invalid-credential':
_emailError = 'E-mail ou password incorretos.';
break;
case 'user-not-found':
_emailError = 'Utilizador não encontrado.';
break;
case 'wrong-password':
_passwordError = 'Palavra-passe incorreta.';
break;
case 'weak-password':
_passwordError = 'A password é demasiado fraca.';
break;
default:
_emailError = 'Erro: $code';
}
}
@override
void dispose() {
emailController.dispose();
passwordController.dispose();
super.dispose();
}
}