# Authentication Error Handling with Internationalization This document describes the enhanced authentication error handling system that provides localized error messages based on the current language setting. ## Overview The authentication context has been updated to handle error messages in multiple languages, providing a better user experience for international users. The system includes: 1. **Comprehensive Error Mapping**: Server errors are mapped to appropriate translation keys 2. **Internationalized Error Messages**: All error messages are displayed in the user's current language 3. **Consistent Error Handling**: Standardized error handling across all authentication operations 4. **Reusable Components**: Pre-built components for displaying errors in different formats ## Features ### 1. Automatic Error Translation The system automatically translates server error responses into the user's current language: ```rust // Before (hardcoded English) s.error = Some("Login failed".to_string()); // After (internationalized) s.error = Some(error_handler.handle_request_failure("login")); ``` ### 2. Smart Error Mapping Server responses are intelligently mapped to appropriate translation keys: - JSON API responses are parsed and mapped - Common error patterns are recognized - Fallback to generic error messages when specific mapping isn't available ### 3. Comprehensive Error Coverage The system handles all types of authentication errors: - **Invalid Credentials**: Wrong username/password - **Token Errors**: Expired or invalid tokens - **Account Issues**: Suspended or unverified accounts - **Network Errors**: Connection problems - **Server Errors**: Internal server errors - **Validation Errors**: Input validation failures ## Available Error Messages ### English (en.ftl) ``` invalid-credentials = Invalid email or password user-not-found = User not found email-already-exists = An account with this email already exists username-already-exists = This username is already taken invalid-token = Invalid authentication token token-expired = Your authentication token has expired insufficient-permissions = You don't have permission to perform this action account-not-verified = Please verify your email before signing in account-suspended = Your account has been suspended rate-limit-exceeded = Too many attempts. Please try again later oauth-error = OAuth authentication error database-error = A database error occurred. Please try again internal-error = An internal error occurred. Please try again validation-error = Please check your input and try again network-error = Network error. Please check your connection login-failed = Login failed registration-failed = Registration failed session-expired = Your session has expired. Please sign in again profile-update-failed = Failed to update profile password-change-failed = Failed to change password server-error = Server error occurred. Please try again later request-failed = Request failed. Please try again unknown-error = An unknown error occurred ``` ### Spanish (es.ftl) ``` invalid-credentials = Correo electrónico o contraseña inválidos user-not-found = Usuario no encontrado email-already-exists = Ya existe una cuenta con este correo electrónico username-already-exists = Este nombre de usuario ya está en uso invalid-token = Token de autenticación inválido token-expired = Tu token de autenticación ha expirado insufficient-permissions = No tienes permisos para realizar esta acción account-not-verified = Por favor verifica tu correo electrónico antes de iniciar sesión account-suspended = Tu cuenta ha sido suspendida rate-limit-exceeded = Demasiados intentos. Por favor intenta de nuevo más tarde oauth-error = Error de autenticación OAuth database-error = Ocurrió un error en la base de datos. Por favor intenta de nuevo internal-error = Ocurrió un error interno. Por favor intenta de nuevo validation-error = Por favor revisa tu información e intenta de nuevo network-error = Error de red. Por favor verifica tu conexión login-failed = Error al iniciar sesión registration-failed = Error en el registro session-expired = Tu sesión ha expirado. Por favor inicia sesión de nuevo profile-update-failed = Error al actualizar el perfil password-change-failed = Error al cambiar la contraseña server-error = Error del servidor. Por favor intenta más tarde request-failed = La solicitud falló. Por favor intenta de nuevo unknown-error = Ocurrió un error desconocido ``` ## Usage ### 1. Basic Error Handling The authentication context automatically handles errors with localization: ```rust use crate::auth::use_auth; #[component] pub fn LoginComponent() -> impl IntoView { let auth = use_auth(); view! {
{error_message}
{network_error}