97 lines
3.0 KiB
Dart
97 lines
3.0 KiB
Dart
|
|
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();
|
|
}
|
|
} |