How Authentication Works
The authentication system follows a token-based approach:Register or Login
You obtain a JWT token by either registering a new account or logging in with existing credentials.
Include in Requests
For protected endpoints, include the token in the
Authorization header of your requests.Authentication Flow
Token Lifecycle
Token Generation
When you successfully log in, the system generates a JWT token containing:- User ID: Unique identifier for the authenticated user
- Role: User role (PATIENT, DOCTOR, or ADMIN)
- Expiration: Token validity period (1 hour)
JWT_SECRET environment variable) to prevent tampering.
Token Validation
The authentication middleware (src/middlewares/auth.js:3-17) validates tokens on protected routes:
- Extracts the token from the
Authorizationheader - Verifies the token signature using the secret key
- Checks if the token has expired
- Attaches user information to the request object
Token Expiration
Currently, the API does not support token refresh. When your token expires, you will receive a403 Forbidden error and need to re-authenticate.
Security Considerations
Password Hashing
All passwords are hashed using bcryptjs with a configurable salt rounds value (default: 10 rounds). Passwords are never stored in plain text.Password Requirements
Passwords must be at least 8 characters long. This is enforced both at the schema validation level and in the service layer.
Token Storage
HTTPS Requirement
Protected Routes
Routes that require authentication use theauthenticateToken middleware. Example from src/routes/auth.js:10-12:
Error Responses
The authentication system returns specific error messages:| Status Code | Error Message | Description |
|---|---|---|
| 401 | Access Denied, no token provided | No token in Authorization header |
| 403 | Invalid token | Token is malformed, expired, or has invalid signature |
| 401 | Usuario y/o contraseña incorrecta | Invalid login credentials |
| 400 | Various validation errors | Missing fields or invalid data format |
Next Steps
Register a User
Learn how to create a new user account
Login
Authenticate and receive a JWT token
JWT Tokens
Understand token structure and usage