stats page pagina melhrar
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
|
||||
class LoginController with ChangeNotifier {
|
||||
final FirebaseAuth _auth = FirebaseAuth.instance;
|
||||
// 1. Substituímos o FirebaseAuth pelo cliente do Supabase
|
||||
final SupabaseClient _supabase = Supabase.instance.client;
|
||||
|
||||
final TextEditingController emailController = TextEditingController();
|
||||
final TextEditingController passwordController = TextEditingController();
|
||||
@@ -22,6 +23,7 @@ class LoginController with ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
// --- VALIDAÇÕES (Mantêm-se iguais) ---
|
||||
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}$');
|
||||
@@ -37,10 +39,17 @@ class LoginController with ChangeNotifier {
|
||||
|
||||
// --- MÉTODO PARA ENTRAR (LOGIN) ---
|
||||
Future<bool> login() async {
|
||||
_emailError = validateEmail(emailController.text);
|
||||
_passwordError = validatePassword(passwordController.text);
|
||||
// Limpa erros anteriores
|
||||
_emailError = null;
|
||||
_passwordError = null;
|
||||
|
||||
// Valida localmente primeiro
|
||||
String? emailValidation = validateEmail(emailController.text);
|
||||
String? passValidation = validatePassword(passwordController.text);
|
||||
|
||||
if (_emailError != null || _passwordError != null) {
|
||||
if (emailValidation != null || passValidation != null) {
|
||||
_emailError = emailValidation;
|
||||
_passwordError = passValidation;
|
||||
notifyListeners();
|
||||
return false;
|
||||
}
|
||||
@@ -49,16 +58,25 @@ class LoginController with ChangeNotifier {
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
await _auth.signInWithEmailAndPassword(
|
||||
// 2. Chamada ao Supabase para Login
|
||||
await _supabase.auth.signInWithPassword(
|
||||
email: emailController.text.trim(),
|
||||
password: passwordController.text.trim(),
|
||||
);
|
||||
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
return true;
|
||||
} on FirebaseAuthException catch (e) {
|
||||
|
||||
} on AuthException catch (e) {
|
||||
// 3. Captura erros específicos do Supabase
|
||||
_isLoading = false;
|
||||
_handleFirebaseError(e.code);
|
||||
_handleSupabaseError(e);
|
||||
notifyListeners();
|
||||
return false;
|
||||
} catch (e) {
|
||||
_isLoading = false;
|
||||
_emailError = 'Ocorreu um erro inesperado.';
|
||||
notifyListeners();
|
||||
return false;
|
||||
}
|
||||
@@ -66,10 +84,15 @@ class LoginController with ChangeNotifier {
|
||||
|
||||
// --- MÉTODO PARA CRIAR CONTA (SIGN UP) ---
|
||||
Future<bool> signUp() async {
|
||||
_emailError = validateEmail(emailController.text);
|
||||
_passwordError = validatePassword(passwordController.text);
|
||||
_emailError = null;
|
||||
_passwordError = null;
|
||||
|
||||
if (_emailError != null || _passwordError != null) {
|
||||
String? emailValidation = validateEmail(emailController.text);
|
||||
String? passValidation = validatePassword(passwordController.text);
|
||||
|
||||
if (emailValidation != null || passValidation != null) {
|
||||
_emailError = emailValidation;
|
||||
_passwordError = passValidation;
|
||||
notifyListeners();
|
||||
return false;
|
||||
}
|
||||
@@ -78,40 +101,46 @@ class LoginController with ChangeNotifier {
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
await _auth.createUserWithEmailAndPassword(
|
||||
// 4. Chamada ao Supabase para Registo
|
||||
await _supabase.auth.signUp(
|
||||
email: emailController.text.trim(),
|
||||
password: passwordController.text.trim(),
|
||||
);
|
||||
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
return true;
|
||||
} on FirebaseAuthException catch (e) {
|
||||
|
||||
} on AuthException catch (e) {
|
||||
_isLoading = false;
|
||||
_handleFirebaseError(e.code);
|
||||
_handleSupabaseError(e);
|
||||
notifyListeners();
|
||||
return false;
|
||||
} catch (e) {
|
||||
_isLoading = false;
|
||||
_emailError = 'Ocorreu um erro inesperado.';
|
||||
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';
|
||||
// --- TRATAMENTO DE ERROS SUPABASE ---
|
||||
void _handleSupabaseError(AuthException error) {
|
||||
// O Supabase retorna mensagens em inglês, vamos traduzir as mais comuns.
|
||||
// O 'message' contém o texto do erro.
|
||||
final msg = error.message.toLowerCase();
|
||||
|
||||
if (msg.contains('invalid login credentials')) {
|
||||
_emailError = 'E-mail ou password incorretos.';
|
||||
} else if (msg.contains('user already registered') || msg.contains('already exists')) {
|
||||
_emailError = 'Este e-mail já está registado.';
|
||||
} else if (msg.contains('password')) {
|
||||
_passwordError = 'A password deve ter pelo menos 6 caracteres.';
|
||||
} else if (msg.contains('email')) {
|
||||
_emailError = 'Formato de e-mail inválido.';
|
||||
} else {
|
||||
// Fallback para mostrar a mensagem original se não conhecermos o erro
|
||||
_emailError = error.message;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user