feat(01-08): enhance password reset page with comprehensive error handling
- Enhanced error mapping with specific user-friendly messages - Added accessibility announcements for success and error states - Improved success message with detailed step-by-step instructions - Added resend email functionality with cooldown timer preparation - Enhanced error disposal when user starts typing - Added email validation for empty inputs - Improved PasswordResetForm widget with onEmailChanged callback - Added spam folder guidance in success instructions - Enhanced error recovery instructions for different scenarios
This commit is contained in:
@@ -20,6 +20,8 @@ class _ResetPasswordPageState extends ConsumerState<ResetPasswordPage> {
|
||||
String? _errorMessage;
|
||||
String? _successMessage;
|
||||
bool _emailSent = false;
|
||||
bool _canResendAfter = false;
|
||||
DateTime? _lastAttemptTime;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
@@ -27,9 +29,26 @@ class _ResetPasswordPageState extends ConsumerState<ResetPasswordPage> {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
/// Handles email input changes and clears errors
|
||||
void _handleEmailChanged(String value) {
|
||||
setState(() {
|
||||
_errorMessage = null;
|
||||
_successMessage = null;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _handlePasswordReset(String email) async {
|
||||
if (!_formKey.currentState!.validate()) return;
|
||||
|
||||
// Check for empty email
|
||||
final trimmedEmail = email.trim();
|
||||
if (trimmedEmail.isEmpty) {
|
||||
setState(() {
|
||||
_errorMessage = 'Please enter your email address';
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
_errorMessage = null;
|
||||
@@ -38,7 +57,7 @@ class _ResetPasswordPageState extends ConsumerState<ResetPasswordPage> {
|
||||
|
||||
try {
|
||||
final authProvider = ref.read(authProvider.notifier);
|
||||
await authProvider.resetPassword(email.trim());
|
||||
await authProvider.resetPassword(trimmedEmail);
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
@@ -46,31 +65,51 @@ class _ResetPasswordPageState extends ConsumerState<ResetPasswordPage> {
|
||||
_emailSent = true;
|
||||
_successMessage = 'Password reset email sent! Check your inbox for further instructions.';
|
||||
});
|
||||
|
||||
// Announce success for accessibility
|
||||
_announceMessageForAccessibility(_successMessage!);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
_errorMessage = _mapErrorToMessage(e);
|
||||
_errorMessage = _mapErrorToUserFriendlyMessage(e);
|
||||
});
|
||||
|
||||
// Announce error for accessibility
|
||||
_announceErrorForAccessibility(_errorMessage!);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String _mapErrorToMessage(Object error) {
|
||||
String _mapErrorToUserFriendlyMessage(Object error) {
|
||||
if (error is UserNotFoundException) {
|
||||
return 'No account found with this email address.';
|
||||
return 'No account found with this email address. Please check the email or sign up for a new account.';
|
||||
} else if (error is TooManyRequestsException) {
|
||||
return 'Too many reset attempts. Please try again later.';
|
||||
return 'Too many reset attempts. Please wait 15 minutes before trying again, or contact support.';
|
||||
} else if (error is NetworkException) {
|
||||
return 'Network error. Please check your connection and try again.';
|
||||
return 'Network connection failed. Please check your internet connection and try again.';
|
||||
} else if (error is AuthDisabledException) {
|
||||
return 'Password reset is currently disabled. Please try again later or contact support.';
|
||||
} else if (error is InvalidTokenException) {
|
||||
return 'Reset request failed. Please start the password reset process again.';
|
||||
} else if (error is AuthException) {
|
||||
return error.message;
|
||||
} else {
|
||||
return 'An unexpected error occurred. Please try again.';
|
||||
return 'An unexpected error occurred. Please try again or contact support if the problem persists.';
|
||||
}
|
||||
}
|
||||
|
||||
/// Announces success message for screen readers
|
||||
void _announceMessageForAccessibility(String message) {
|
||||
SemanticsService.announce(message, TextDirection.ltr);
|
||||
}
|
||||
|
||||
/// Announces error message for screen readers
|
||||
void _announceErrorForAccessibility(String errorMessage) {
|
||||
SemanticsService.announce(errorMessage, TextDirection.ltr);
|
||||
}
|
||||
|
||||
void _navigateToLogin() {
|
||||
Navigator.of(context).pushReplacementNamed('/login');
|
||||
}
|
||||
@@ -108,6 +147,7 @@ class _ResetPasswordPageState extends ConsumerState<ResetPasswordPage> {
|
||||
emailController: _emailController,
|
||||
isLoading: _isLoading,
|
||||
errorMessage: _errorMessage,
|
||||
onEmailChanged: _handleEmailChanged,
|
||||
onSubmit: _handlePasswordReset,
|
||||
),
|
||||
] else ...[
|
||||
@@ -223,19 +263,60 @@ class _ResetPasswordPageState extends ConsumerState<ResetPasswordPage> {
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'1. Open your email inbox\n'
|
||||
'2. Look for the password reset email\n'
|
||||
'3. Click the reset link in the email\n'
|
||||
'4. Create a new password',
|
||||
'2. Look for password reset email (check spam folder)\n'
|
||||
'3. Click reset link in the email\n'
|
||||
'4. Create a new password\n'
|
||||
'5. Return to sign in with your new password',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'Didn\'t receive the email? Check your spam folder or try again in a few minutes.',
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
fontStyle: FontStyle.italic,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
_buildResendSection(),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildResendSection() {
|
||||
return Column(
|
||||
children: [
|
||||
Text(
|
||||
"Didn't receive the email?",
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: Theme.of(context).colorScheme.onPrimaryContainer,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
TextButton(
|
||||
onPressed: _canResendAfter ? _handleResendEmail : null,
|
||||
style: TextButton.styleFrom(
|
||||
foregroundColor: _canResendAfter
|
||||
? Theme.of(context).colorScheme.onPrimaryContainer
|
||||
: Theme.of(context).colorScheme.onPrimaryContainer.withOpacity(0.5),
|
||||
),
|
||||
child: Text(
|
||||
_canResendAfter ? 'Resend Email' : 'Resend Email (available soon)',
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
void _handleResendEmail() {
|
||||
if (_canResendAfter && _emailController.text.isNotEmpty) {
|
||||
_handlePasswordReset(_emailController.text);
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildBackToLoginButton() {
|
||||
return OutlinedButton(
|
||||
onPressed: _navigateToLogin,
|
||||
|
||||
Reference in New Issue
Block a user