Authentication Mechanism
ICL Cotizaciones uses iron-session for encrypted, stateless session management via HTTP-only cookies.| Aspect | Implementation |
|---|---|
| Type | Encrypted HTTP-only cookie (iron-session v8) |
| Cookie name | icl-session |
| Encryption | Symmetric AES with configurable password |
| Password hashing | bcryptjs with compareSync() |
| Page guards | Server-side redirect in (dashboard)/layout.tsx |
| API guards | Manual session.isLoggedIn check in each route handler |
| Middleware | Not implemented — no middleware.ts file |
Session Configuration
The session is configured insrc/lib/session.ts:sessionOptions:14:
The
secure: false setting allows session cookies to work over HTTP during local development. Always set to true in production.User Roles
Roles are defined insrc/lib/utils.ts:32 and stored in the users.role column.
| Role | Type | Capabilities |
|---|---|---|
DIRECTOR | Admin | Full system access |
GERENTE | Admin | Full system access |
ADMINISTRACION | Admin | Full system access |
COMERCIAL | Standard | Own quotations only |
OPERACIONES | Standard | Own quotations only |
Admin Check
isAdmin() function is used throughout API routes to restrict access to master data mutations.
Authentication Flows
Login Flow
Page Guard (Layout-Based)
All protected routes under(dashboard)/ are guarded by the layout server component.
Implementation:
API Route Guard
API routes manually check session status and authorization. Example API Guard:Logout Flow
Root Page Redirect
The root page (/) redirects based on session status.
Implementation:
Authorization Patterns
Admin-Only Endpoints
Master data endpoints (clients, users, locations, agreements, pricing) require admin role.Ownership-Based Access
Non-admin users can only access their own quotations.UI Access Control
The sidebar conditionally shows master data sections based on role.Pages themselves are client components without built-in guards. Effective protection is enforced at the API level. Non-admin users who manually navigate to
/maestros/* URLs will see the page but cannot mutate data.Security Considerations
CSRF Protection
CSRF Protection
iron-session with
sameSite: "lax" provides automatic CSRF protection for most cross-site attacks. For additional protection in production, consider implementing CSRF tokens for state-changing operations.Password Security
Password Security
Passwords are hashed with
bcryptjs before storage. Never log or expose password hashes. Use strong password requirements in production (minimum length, complexity rules).Session Expiration
Session Expiration
iron-session supports
maxAge for automatic session expiration. Currently not configured — sessions persist until browser close or manual logout. Consider adding maxAge: 60 * 60 * 24 * 7 (7 days) for production.Secure Flag
Secure Flag
The
secure: false cookie flag must be changed to true in production to prevent session hijacking over unencrypted connections.Environment Variables
Environment Variables
Move the session password from hardcoded value to environment variable:Generate a secure random key with:
API Endpoints
| Endpoint | Methods | Access Control |
|---|---|---|
/api/auth/login | POST | Public |
/api/auth/logout | POST | Authenticated |
/api/auth/me | GET | Authenticated |
/api/cotizaciones | GET | Filtered by user_id for non-admin |
/api/cotizaciones | POST | Any authenticated user |
/api/cotizaciones/[id] | GET, PUT, DELETE | Owner or admin |
/api/clientes | GET | Any authenticated user |
/api/clientes | POST, PUT, DELETE | Admin only |
/api/usuarios | GET, POST, PUT, DELETE | Admin only |
All /api/maestros/* | POST, PUT, DELETE | Admin only |
Related Documentation
Session Config
View session configuration in
src/lib/session.tsUtils
See
isAdmin() and role types in src/lib/utils.tsAPI Reference
Explore authentication API endpoints
iron-session Docs
Official iron-session documentation