124 lines
3.4 KiB
Dart
124 lines
3.4 KiB
Dart
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();
|
|
|
|
bool _isLoading = false;
|
|
bool _obscurePassword = true;
|
|
String? _emailError;
|
|
String? _passwordError;
|
|
|
|
bool get isLoading => _isLoading;
|
|
bool get obscurePassword => _obscurePassword;
|
|
String? get emailError => _emailError;
|
|
String? get passwordError => _passwordError;
|
|
|
|
void togglePasswordVisibility() {
|
|
_obscurePassword = !_obscurePassword;
|
|
notifyListeners();
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
// --- MÉTODO PARA ENTRAR (LOGIN) ---
|
|
Future<bool> login() async {
|
|
_emailError = validateEmail(emailController.text);
|
|
_passwordError = validatePassword(passwordController.text);
|
|
|
|
if (_emailError != null || _passwordError != null) {
|
|
notifyListeners();
|
|
return false;
|
|
}
|
|
|
|
_isLoading = true;
|
|
notifyListeners();
|
|
|
|
try {
|
|
await _auth.signInWithEmailAndPassword(
|
|
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;
|
|
}
|
|
}
|
|
|
|
// --- 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();
|
|
}
|
|
} |