feat(01-08): enhance login page with comprehensive error handling

- Connected to AuthProvider for real authentication
- Added specific error messages for different exception types
- Implemented error message display with proper styling
- Added accessibility error announcements via Semantics
- Enhanced form validation with empty field checks
- Connected to real authentication flow instead of simulation
- Added error disposal when user starts typing
- Included loading state management
This commit is contained in:
Dani B
2026-01-28 12:27:33 -05:00
parent 66ea5082eb
commit bd21a62fe5

View File

@@ -1,24 +1,26 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
import '../widgets/auth_form.dart'; import '../widgets/auth_form.dart';
import '../widgets/auth_button.dart'; import '../widgets/auth_button.dart';
import '../../../../providers/auth_provider.dart';
import '../../../../core/errors/auth_exceptions.dart';
/// Login screen with email/password authentication /// Login screen with email/password authentication
class LoginPage extends StatefulWidget { class LoginPage extends ConsumerStatefulWidget {
const LoginPage({super.key}); const LoginPage({super.key});
@override @override
State<LoginPage> createState() => _LoginPageState(); ConsumerState<LoginPage> createState() => _LoginPageState();
} }
class _LoginPageState extends State<LoginPage> { class _LoginPageState extends ConsumerState<LoginPage> {
final _formKey = GlobalKey<FormState>(); final _formKey = GlobalKey<FormState>();
final _emailController = TextEditingController(); final _emailController = TextEditingController();
final _passwordController = TextEditingController(); final _passwordController = TextEditingController();
bool _isLoading = false; bool _isLoading = false;
String? _emailError; String? _errorMessage;
String? _passwordError;
@override @override
void dispose() { void dispose() {
@@ -79,19 +81,19 @@ class _LoginPageState extends State<LoginPage> {
email: _emailController.text, email: _emailController.text,
onEmailChanged: (value) { onEmailChanged: (value) {
setState(() { setState(() {
_emailError = null; _errorMessage = null;
_emailController.text = value; _emailController.text = value;
}); });
}, },
emailError: _emailError, emailError: null,
password: _passwordController.text, password: _passwordController.text,
onPasswordChanged: (value) { onPasswordChanged: (value) {
setState(() { setState(() {
_passwordError = null; _errorMessage = null;
_passwordController.text = value; _passwordController.text = value;
}); });
}, },
passwordError: _passwordError, passwordError: null,
onSubmit: _handleLogin, onSubmit: _handleLogin,
submitButtonText: 'Sign In', submitButtonText: 'Sign In',
isLoading: _isLoading, isLoading: _isLoading,
@@ -99,6 +101,40 @@ class _LoginPageState extends State<LoginPage> {
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
// Error message display
if (_errorMessage != null) ...[
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.errorContainer,
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: Theme.of(context).colorScheme.error,
width: 1,
),
),
child: Row(
children: [
Icon(
Icons.error_outline,
size: 20,
color: Theme.of(context).colorScheme.error,
),
const SizedBox(width: 8),
Expanded(
child: Text(
_errorMessage!,
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: Theme.of(context).colorScheme.onErrorContainer,
),
),
),
],
),
),
const SizedBox(height: 16),
],
// Forgot password link // Forgot password link
Align( Align(
alignment: Alignment.centerRight, alignment: Alignment.centerRight,
@@ -149,8 +185,7 @@ class _LoginPageState extends State<LoginPage> {
Future<void> _handleLogin() async { Future<void> _handleLogin() async {
// Clear previous errors // Clear previous errors
setState(() { setState(() {
_emailError = null; _errorMessage = null;
_passwordError = null;
}); });
// Validate form // Validate form
@@ -158,32 +193,71 @@ class _LoginPageState extends State<LoginPage> {
return; return;
} }
// Check for empty fields
final email = _emailController.text.trim();
final password = _passwordController.text;
if (email.isEmpty || password.isEmpty) {
setState(() {
_errorMessage = 'Please enter both email and password';
});
return;
}
// Show loading state // Show loading state
setState(() { setState(() {
_isLoading = true; _isLoading = true;
}); });
try { try {
// TODO: Implement actual authentication logic final authProvider = ref.read(authProvider.notifier);
// For now, simulate API call await authProvider.signIn(email, password);
await Future.delayed(const Duration(seconds: 2));
// Simulate successful login
if (mounted) { if (mounted) {
// Navigate to home or dashboard // Navigate to home or dashboard
context.go('/home'); context.go('/home');
} }
} catch (e) { } catch (e) {
// Handle authentication errors if (mounted) {
setState(() { setState(() {
_isLoading = false; _isLoading = false;
_errorMessage = _mapErrorToUserFriendlyMessage(e);
// TODO: Implement proper error handling
// For now, show generic error
_passwordError = 'Invalid email or password';
}); });
// Announce error for accessibility
_announceErrorForAccessibility(_errorMessage!);
} }
} }
}
/// Maps authentication exceptions to user-friendly error messages
String _mapErrorToUserFriendlyMessage(Object error) {
if (error is InvalidCredentialsException) {
return 'Invalid password. Please check your password and try again.';
} else if (error is UserNotFoundException) {
return 'Account not found. Please check your email address or sign up for a new account.';
} else if (error is NetworkException) {
return 'Network connection failed. Please check your internet connection and try again.';
} else if (error is SessionExpiredException) {
return 'Your session has expired. Please sign in again.';
} else if (error is EmailNotVerifiedException) {
return 'Please verify your email address before signing in. Check your inbox for verification email.';
} else if (error is AuthDisabledException) {
return 'Authentication is currently disabled. Please try again later.';
} else if (error is TooManyRequestsException) {
return 'Too many login attempts. Please wait a moment and try again.';
} else if (error is AuthException) {
return error.message;
} else {
return 'An unexpected error occurred. Please try again.';
}
}
/// Announces error message for screen readers
void _announceErrorForAccessibility(String errorMessage) {
// Use Semantics to announce the error for screen readers
SemanticsService.announce(errorMessage, TextDirection.ltr);
}
void _handleForgotPassword() { void _handleForgotPassword() {
// Navigate to password reset page // Navigate to password reset page