Overview
OdontologyApp uses a secure authentication system with bcrypt password hashing and HTTP-only cookie-based sessions. All authentication endpoints are protected and include comprehensive logging for security auditing.Login Process
Authentication Endpoint
The login process is handled by the/api/auth/login endpoint:
Location: src/routes/api/auth/login/+server.js
Login Flow
Database Lookup
The system calls the stored procedure
sp_authenticate_user(?) to retrieve the user record from the database.Password Verification
The submitted password is compared against the stored bcrypt hash:If the password doesn’t match, the system returns a 401 error.
Session Creation
Upon successful authentication, a session object is created with the following data:
Session Management
Session Cookie Configuration
Sessions are stored with the following security settings:| Setting | Value | Purpose |
|---|---|---|
httpOnly | true | Prevents JavaScript access to the cookie |
sameSite | lax | CSRF protection while allowing normal navigation |
secure | false | Allows HTTP access for local network deployments |
maxAge | 86400 (24 hours) | Session expires after 24 hours |
path | / | Cookie available across the entire application |
The
secure flag is set to false to support IP-based access on local networks. For production deployments with HTTPS, this should be set to true.Session Hook
The application uses a server-side hook to manage sessions on every request: Location:src/hooks.server.js
Session Validation
On each request, the hook:- Retrieves the session cookie
- Parses the JSON data and attaches it to
event.locals.user - Redirects authenticated users away from
/loginto/dashboard - Redirects unauthenticated users to
/loginfor protected routes - Enforces role-based route protection
Protected Routes
The following routes are restricted to users with theadmin role:
/admin/settings/admin/treatments/admin/reports/users/branches/logs
/dashboard?error=unauthorized.
Password Security
Bcrypt Hashing
All passwords are hashed using bcrypt with a cost factor of 10:Bcrypt automatically generates a salt for each password, making rainbow table attacks ineffective. The cost factor of 10 provides a good balance between security and performance.
Default Test Accounts
The system includes three test accounts with bcrypt-hashed passwords:| Username | Password | Role | Hash (truncated) |
|---|---|---|---|
admin | admin123 | admin | $2b$10$mo1jWoA/zRY1wN6x6hsfge... |
doctor | doctor123 | doctor | $2b$10$aCdT80m.xivVAK0rWf.rD.... |
secretaria | secretaria123 | secretary | $2b$10$Zs8LX/f2nidTFspYT6QQEO... |
Logout
The logout process is handled by sending a DELETE request to/api/auth/login:
Security Logging
Login Audit Trail
Every successful login is logged to thelogs table:
- User ID
- Action type (
Login) - Module (
Auth) - Description
- IP address (from
x-forwarded-forheader or defaults to127.0.0.1)
Error Handling
The authentication system returns specific error messages:| HTTP Code | Message | Cause |
|---|---|---|
| 400 | ”Falta usuario o contraseña” | Missing credentials |
| 401 | ”Usuario no encontrado” | Invalid username |
| 401 | ”Contraseña incorrecta” | Invalid password |
| 500 | ”Error del servidor: [details]“ | Database or server error |
Best Practices
Strong Passwords
Enforce minimum password length and complexity requirements when creating user accounts.
Regular Audits
Regularly review the logs table to detect suspicious login patterns or unauthorized access attempts.
Session Timeouts
The 24-hour session timeout helps ensure inactive users are automatically logged out.
HTTPS in Production
Always enable HTTPS and set the
secure cookie flag to true in production environments.Related Resources
Roles & Permissions
Learn about the three user roles and their capabilities
User Management
Managing doctor accounts and credentials
