firebase esta feita acabar o register
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
97
lib/controllers/register_controller.dart
Normal file
97
lib/controllers/register_controller.dart
Normal file
@@ -0,0 +1,97 @@
|
||||
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class RegisterController with ChangeNotifier {
|
||||
final FirebaseAuth _auth = FirebaseAuth.instance;
|
||||
|
||||
final TextEditingController emailController = TextEditingController();
|
||||
final TextEditingController passwordController = TextEditingController();
|
||||
final TextEditingController confirmPasswordController = TextEditingController();
|
||||
|
||||
bool _isLoading = false;
|
||||
String? _emailError;
|
||||
String? _passwordError;
|
||||
String? _confirmPasswordError; // Novo!
|
||||
String? get confirmPasswordError => _confirmPasswordError; // Novo!
|
||||
|
||||
bool get isLoading => _isLoading;
|
||||
String? get emailError => _emailError;
|
||||
String? get passwordError => _passwordError;
|
||||
|
||||
// Validações
|
||||
String? validateEmail(String? value) {
|
||||
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';
|
||||
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';
|
||||
return null;
|
||||
}
|
||||
String? validateConfirmPassword(String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Por favor, confirme a sua password';
|
||||
}
|
||||
if (value != passwordController.text) {
|
||||
return 'As passwords não coincidem';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
// MÉTODO PARA CRIAR CONTA (SIGN UP)
|
||||
Future<bool> signUp() async {
|
||||
_emailError = validateEmail(emailController.text);
|
||||
_passwordError = validatePassword(passwordController.text);
|
||||
_emailError = validateEmail(emailController.text);
|
||||
_passwordError = validatePassword(passwordController.text);
|
||||
_confirmPasswordError = validateConfirmPassword(confirmPasswordController.text); // Valida aqui!
|
||||
|
||||
if (_emailError != null || _passwordError != null || _confirmPasswordError != 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 'weak-password':
|
||||
_passwordError = 'A password é demasiado fraca.';
|
||||
break;
|
||||
default:
|
||||
_emailError = 'Erro ao registar: $code';
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
emailController.dispose();
|
||||
passwordController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user