useSeguridad hook manages all security-related functionality including security PIN (NIP) creation/modification, password changes, and biometric authentication (Face ID/Touch ID/Fingerprint).
Overview
This hook provides comprehensive account security management:- Create and validate 4-digit security PIN (NIP)
- Change existing PIN with email verification
- Update Firebase Authentication password
- Enable/disable biometric authentication
- Multi-step security flows with email verification
The security PIN is separate from the login password and is used for sensitive operations like creating payment cards and confirming reservations.
Import
Usage
State Values
Current step in the security flow:
'loading'- Initial load, checking NIP existence'menu'- Main security menu (NIP exists)'verify_email'- Email verification for new NIP'create_nip'- Creating new NIP'confirm_nip'- Confirming new NIP'validate_nip_for_pass'- Validating NIP before password change'change_password'- Password change form
User input for 6-digit email verification code
Generated 6-digit verification code sent to user’s email
Loading state for async operations
Array of 4 digits representing the security PIN (e.g.,
['1', '2', '3', '4'])Current user’s email address from Firebase Auth
Whether biometric authentication is enabled for this user
Whether device supports biometric authentication (hardware + enrollment check)
New password input for password change flow
Functions
handleSendCode
Generates and sends a 6-digit verification code to user’s email.- Generates random 6-digit code
- Sends email via EmailJS with template
- Stores code in
generatedCodestate - Shows success/error alert
verifyEmailCode
Validates the email verification code entered by user.- Compares
emailCodeInputwithgeneratedCode - If match → advances to
'create_nip'step - If no match → shows error alert
handlePressNumber
Adds a digit to the current NIP input.Digit to add (0-9)
- Finds first empty slot in
niparray - Fills with the pressed number
- Auto-advances to confirmation when 4 digits entered
handleDelete
Removes the last digit from NIP input.nip array.
toggleBiometrics
Enables or disables biometric authentication.- Prompts biometric authentication (
LocalAuthentication.authenticateAsync) - If successful → sets
biometricsEnabledtotrue - Updates Firestore:
users/[uid]/biometricsEnabled = true - Shows success alert
- Sets
biometricsEnabledtofalse - Updates Firestore immediately
- No authentication required to disable
startChangeNipProcess
Initiates the NIP change workflow.- Sends email verification code
- User enters code
- User enters new NIP
- User confirms new NIP
- NIP saved to Firestore
handleRecoverNip
Handles NIP recovery (same as change, but for forgotten NIP).startChangePasswordProcess
Initiates password change workflow.- User validates current NIP
- If valid → shows password change form
- User enters new password (min 6 characters)
- Password updated via Firebase Auth
handleUpdatePassword
Updates the user’s Firebase Authentication password.- Password must be at least 6 characters
- Uses Firebase
updatePassword()function
Complete Example
SeguridadScreen.tsx
Firebase Structure
users Collection
The NIP is stored as plain text in Firestore since it’s not used for authentication, only for operation confirmation. For production, consider hashing the NIP.
Security Flows
First-Time NIP Creation
Change Existing NIP
Change Password
Platform Support
| Feature | iOS | Android | Web |
|---|---|---|---|
| Face ID | ✅ | N/A | N/A |
| Touch ID | ✅ | N/A | N/A |
| Fingerprint | N/A | ✅ | N/A |
| NIP | ✅ | ✅ | ✅ |
| Password | ✅ | ✅ | ✅ |
Related
- User Profile Feature - Account settings
- useLogin Hook - Authentication
- Payment Methods Guide - NIP used for card creation