Overview
TheuseLogin hook manages the complete authentication flow including traditional login with username or email, intelligent password recovery with email verification codes, and failed attempt tracking. It integrates with Firebase Authentication and Firestore for user management.
Import
Usage
State Values
Login State
User’s email address or username for login
User’s password for authentication
Indicates if login operation is in progress
Counter tracking number of failed login attempts in current session
Recovery State
Controls visibility of the password recovery modal
Current step in the recovery flow:
verify_code: User must enter the email verification codereset_link: Final step showing reset link was sent
The full email address found during recovery process (internal use)
Censored email shown to user (e.g., “us***@gmail.com”)
The 4-digit verification code sent via email (internal use)
User’s input for the verification code
Indicates if recovery operation is in progress
Functions
setIdentifier
Email address or username
setPassword
User’s password
handleLogin
- Validates that both identifier and password are provided
- Detects if identifier is email (contains ”@”) or username
- If username, queries Firestore to find associated email:
- Authenticates using Firebase Auth with email and password
- On success:
- Resets failed attempts counter to 0
- Navigates to
/inicioroute
- On failure:
- Increments failed attempts counter
- Shows appropriate error message based on error code
auth/invalid-credential: “Credenciales incorrectas.”auth/wrong-password: “Contraseña incorrecta.”- Default: “Error al iniciar sesión.”
handleStartRecovery
- Validates that identifier field is filled
- Determines if input is email or username
- Queries Firestore to verify account exists:
- For email:
where("email", "==", identifier) - For username:
where("username", "==", identifier)
- For email:
- If account found:
- Masks email for privacy (e.g., “ab***@domain.com”)
- Generates random 4-digit code
- Sends verification code via EmailJS
- Opens recovery modal on
verify_codestep
- If not found:
- Shows appropriate error message
handleVerifyRecoverCode
- If code matches: Calls
handleSendFinalResetLink - If code doesn’t match: Shows “Código incorrecto” alert
handleSendFinalResetLink
- Calls Firebase Auth’s
sendPasswordResetEmail(auth, recoverEmail) - Updates modal step to
reset_link - User can then check their email for the secure reset link
- Generates a secure, time-limited token
- Sends official password reset link
- Ensures secure password update flow
setRecoverModalVisible
setInputCode
Email Integration
The hook uses EmailJS to send verification codes during password recovery:- Service ID:
service_ov1txor - Template ID:
template_tn3fdwk - Public Key:
uwyrohVqlzvgOj1KT
Example: Login Form
Example: Password Recovery Flow
Security Features
Failed Attempts Tracking
The hook tracks failed login attempts to help detect potential security issues:- Account lockout after X attempts
- CAPTCHA triggers
- Security notifications
Two-Factor Email Verification
Password recovery requires email verification before sending reset link:- User enters identifier
- System generates 4-digit code
- Code sent to registered email
- User must enter correct code
- Only then is Firebase reset link sent
Email Masking
Email addresses are masked when displayed to prevent information leakage:- Shows only first 2 characters of local part
- Keeps full domain for context
- Example:
[email protected]→us***@example.com
Error Handling
The hook handles various error scenarios:Login Errors
- Empty fields: Alert prompting for both identifier and password
- Username not found: “El nombre de usuario no existe.”
- Invalid credentials: “Credenciales incorrectas.”
- Wrong password: “Contraseña incorrecta.”
- Generic errors: “Error al iniciar sesión.”
Recovery Errors
- Empty identifier: “Escribe tu Usuario o Correo en el campo de arriba para buscar tu cuenta.”
- Email not found: “Ese correo electrónico no está registrado en ParkInMX.”
- Username not found: “Ese nombre de usuario no existe.”
- Email send failure: “No se pudo generar el enlace de cambio.”
- Generic errors: “Ocurrió un problema al buscar tu cuenta.”
Firebase Dependencies
- Authentication:
signInWithEmailAndPassword,sendPasswordResetEmail - Firestore Collections:
users - User document fields:
username,email
Navigation
On successful login, the hook navigates to the home screen:replace to prevent users from going back to login screen after authentication.
Related Hooks
- useReservar - Requires authentication to create reservations
- useHistorial - View user’s reservation history
- useTarjetas - Manage payment methods