feat(01-08): enhance signup page with comprehensive error handling
- Connected to AuthProvider for real registration - Added specific error messages for different exception types - Implemented password mismatch validation before submission - Added error message display with proper styling - Enhanced form validation with empty field checks - Added accessibility error announcements via Semantics - Connected to real registration flow instead of simulation - Added success message with quick navigation to login - Included terms agreement validation with clear error message
This commit is contained in:
@@ -1,26 +1,27 @@
|
|||||||
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';
|
||||||
|
|
||||||
/// Sign up screen with user registration
|
/// Sign up screen with user registration
|
||||||
class SignupPage extends StatefulWidget {
|
class SignupPage extends ConsumerStatefulWidget {
|
||||||
const SignupPage({super.key});
|
const SignupPage({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<SignupPage> createState() => _SignupPageState();
|
ConsumerState<SignupPage> createState() => _SignupPageState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _SignupPageState extends State<SignupPage> {
|
class _SignupPageState extends ConsumerState<SignupPage> {
|
||||||
final _formKey = GlobalKey<FormState>();
|
final _formKey = GlobalKey<FormState>();
|
||||||
final _emailController = TextEditingController();
|
final _emailController = TextEditingController();
|
||||||
final _passwordController = TextEditingController();
|
final _passwordController = TextEditingController();
|
||||||
final _confirmPasswordController = TextEditingController();
|
final _confirmPasswordController = TextEditingController();
|
||||||
|
|
||||||
bool _isLoading = false;
|
bool _isLoading = false;
|
||||||
String? _emailError;
|
String? _errorMessage;
|
||||||
String? _passwordError;
|
|
||||||
String? _confirmPasswordError;
|
|
||||||
bool _agreedToTerms = false;
|
bool _agreedToTerms = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -83,27 +84,27 @@ class _SignupPageState extends State<SignupPage> {
|
|||||||
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,
|
||||||
confirmPassword: _confirmPasswordController.text,
|
confirmPassword: _confirmPasswordController.text,
|
||||||
onConfirmPasswordChanged: (value) {
|
onConfirmPasswordChanged: (value) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_confirmPasswordError = null;
|
_errorMessage = null;
|
||||||
_confirmPasswordController.text = value;
|
_confirmPasswordController.text = value;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
confirmPasswordError: _confirmPasswordError,
|
confirmPasswordError: null,
|
||||||
onSubmit: _handleSignup,
|
onSubmit: _handleSignup,
|
||||||
submitButtonText: 'Create Account',
|
submitButtonText: 'Create Account',
|
||||||
isLoading: _isLoading,
|
isLoading: _isLoading,
|
||||||
@@ -111,6 +112,40 @@ class _SignupPageState extends State<SignupPage> {
|
|||||||
),
|
),
|
||||||
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),
|
||||||
|
],
|
||||||
|
|
||||||
// Terms and conditions checkbox
|
// Terms and conditions checkbox
|
||||||
_buildTermsCheckbox(),
|
_buildTermsCheckbox(),
|
||||||
const SizedBox(height: 24),
|
const SizedBox(height: 24),
|
||||||
@@ -225,20 +260,110 @@ class _SignupPageState extends State<SignupPage> {
|
|||||||
Future<void> _handleSignup() async {
|
Future<void> _handleSignup() async {
|
||||||
// Clear previous errors
|
// Clear previous errors
|
||||||
setState(() {
|
setState(() {
|
||||||
_emailError = null;
|
_errorMessage = null;
|
||||||
_passwordError = null;
|
|
||||||
_confirmPasswordError = null;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Get form values
|
||||||
|
final email = _emailController.text.trim();
|
||||||
|
final password = _passwordController.text;
|
||||||
|
final confirmPassword = _confirmPasswordController.text;
|
||||||
|
|
||||||
// Validate terms agreement
|
// Validate terms agreement
|
||||||
if (!_agreedToTerms) {
|
if (!_agreedToTerms) {
|
||||||
|
setState(() {
|
||||||
|
_errorMessage = 'Please agree to Terms of Service and Privacy Policy';
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check password match before form validation
|
||||||
|
if (password != confirmPassword) {
|
||||||
|
setState(() {
|
||||||
|
_errorMessage = 'Passwords do not match. Please ensure both passwords are identical.';
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate form
|
||||||
|
if (!_formKey.currentState!.validate()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for empty fields
|
||||||
|
if (email.isEmpty || password.isEmpty || confirmPassword.isEmpty) {
|
||||||
|
setState(() {
|
||||||
|
_errorMessage = 'Please fill in all required fields';
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Show loading state
|
||||||
|
setState(() {
|
||||||
|
_isLoading = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
final authProvider = ref.read(authProvider.notifier);
|
||||||
|
await authProvider.signUp(email, password);
|
||||||
|
|
||||||
|
if (mounted) {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
const SnackBar(
|
SnackBar(
|
||||||
content: Text('Please agree to the Terms of Service and Privacy Policy'),
|
content: const Text('Account created successfully! Please sign in.'),
|
||||||
duration: Duration(seconds: 3),
|
duration: const Duration(seconds: 3),
|
||||||
|
backgroundColor: Colors.green,
|
||||||
|
action: SnackBarAction(
|
||||||
|
label: 'Sign In',
|
||||||
|
onPressed: () => context.go('/login'),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
return;
|
|
||||||
|
// Navigate to login page after a short delay
|
||||||
|
Future.delayed(const Duration(seconds: 1), () {
|
||||||
|
if (mounted) {
|
||||||
|
context.go('/login');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
if (mounted) {
|
||||||
|
setState(() {
|
||||||
|
_isLoading = false;
|
||||||
|
_errorMessage = _mapErrorToUserFriendlyMessage(e);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Announce error for accessibility
|
||||||
|
_announceErrorForAccessibility(_errorMessage!);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Maps authentication exceptions to user-friendly error messages
|
||||||
|
String _mapErrorToUserFriendlyMessage(Object error) {
|
||||||
|
if (error is EmailAlreadyInUseException) {
|
||||||
|
return 'This email address is already registered. Please use a different email or try signing in.';
|
||||||
|
} else if (error is WeakPasswordException) {
|
||||||
|
return 'Password is too weak. Please use a stronger password with at least 8 characters, including uppercase, lowercase, and numbers.';
|
||||||
|
} else if (error is InvalidCredentialsException) {
|
||||||
|
return 'Invalid email format. Please enter a valid email address.';
|
||||||
|
} else if (error is NetworkException) {
|
||||||
|
return 'Network connection failed. Please check your internet connection and try again.';
|
||||||
|
} else if (error is TooManyRequestsException) {
|
||||||
|
return 'Too many registration attempts. Please wait a moment and try again.';
|
||||||
|
} else if (error is AuthDisabledException) {
|
||||||
|
return 'Account registration is currently disabled. Please try again later.';
|
||||||
|
} else if (error is AuthException) {
|
||||||
|
return error.message;
|
||||||
|
} else {
|
||||||
|
return 'Registration failed. Please try again or contact support if the problem persists.';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Announces error message for screen readers
|
||||||
|
void _announceErrorForAccessibility(String errorMessage) {
|
||||||
|
// Use Semantics to announce error for screen readers
|
||||||
|
SemanticsService.announce(errorMessage, TextDirection.ltr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate form
|
// Validate form
|
||||||
|
|||||||
Reference in New Issue
Block a user