Authentication Module
Relevant source files
The following files were used as context for generating this wiki page:
The Authentication Module in ElmecV2 provides a secure and robust gateway for user access control, utilizing Supabase as the underlying Backend-as-a-Service. It manages the entire identity lifecycle, including user registration, login with multi-role support (Admin, Agent, Customer), session persistence, and secure logout. The module is designed with built-in timeouts and comprehensive error handling to ensure a smooth user experience even in varying network conditions.
This system integrates directly with the Application Dashboard and uses Row Level Security (RLS) to ensure that authenticated users only access data permitted by their assigned roles.
Sources: VALIDACION-COMPLETA.md, README.md
Architecture and Core Components
The authentication architecture is centralized around a React Context provider that wraps the application, maintaining a global state of the user's authentication status and profile information.
AuthContext
The AuthContext is the heart of the module. It interacts directly with the Supabase Auth API to manage sessions and user profiles. It provides methods for logging in, signing up, and signing out, while also tracking the loading state of the authentication process.
Sources: contexts/AuthContext.tsx, VALIDACION-COMPLETA.md
Supabase Integration
The module utilizes standard JWT-based authentication and persistent sessions. It is configured with a 15-second timeout for robust error handling during the authentication flow.
Sources: VALIDACION-COMPLETA.md, TEST_REPORT.md
This diagram illustrates the flow from the user interface through the AuthContext logic to the Supabase backend services.
User Roles and Data Model
The system supports three primary roles, each defined by metadata stored within the authentication system and a corresponding record in the users table.
| Role | Access Level | Description |
|---|---|---|
| Admin | Full Access | Complete system supervision, user management, and detailed metrics. |
| Agent | Operational Access | Management of assigned requests and real-time chat with customers. |
| Customer | Limited Access | Creation and tracking of personal requests and integration with the calculator. |
Sources: README.md, scripts/setup-supabase-complete.js
User Metadata
During registration or setup, users are assigned specific metadata fields including nombre (first name), apellido_paterno (paternal last name), and rol (role).
Sources: scripts/setup-supabase-complete.js:46-75
Authentication Flows
Login Process
The login process validates user credentials against Supabase. Upon success, it retrieves the user profile and redirects the user based on their authentication status. If the user is already authenticated, the system uses a redirect logic to bypass the login screen.
Sources: FIXES-APLICADOS.md:145-160, app/auth/login.tsx
The sequence above details the communication between the UI, the context provider, and the Supabase backend during a sign-in attempt.
Sources: REPORTE-EJECUCION-PRUEBAS.md, contexts/AuthContext.tsx
Session Persistence and Health
The module includes a health check hook, useSupabaseHealth, which monitors the connectivity and status of the Supabase client. This ensures that the application can react to network issues or service outages.
Sources: hooks/useSupabaseHealth.ts
Security Implementation
The authentication module is reinforced by several security layers:
- JWT Tokens: Used for all authenticated requests to the backend.
- Row Level Security (RLS): Policies are configured to restrict data access based on the
auth.uid()of the requester. - Input Validation: Strict validation for emails and passwords (e.g., rejecting empty fields) is implemented at both the UI and logic levels.
Sources: README.md, REPORTE-EJECUCION-PRUEBAS.md:38-45
Validation Metrics
The following tests have been successfully executed to ensure module reliability:
- TC-AUTH-001: Successful login with valid credentials.
- TC-AUTH-002: Rejection of invalid email formats.
- TC-AUTH-008/009: Rejection of empty email or password fields.
- TC-AUTH-006: Automatic token refreshing.
Sources: REPORTE-EJECUCION-PRUEBAS.md:38-51
Summary
The Authentication Module provides the foundational security layer for ElmecV2. By centralizing logic in AuthContext and leveraging Supabase's identity services, the application achieves a highly secure, role-based access control system with features like session persistence and real-time health monitoring. This module ensures that all user interactions, from managing requests to real-time chatting, are performed within a verified and secure context.
Sources: VALIDACION-COMPLETA.md, README.md