Endpoint
This is a public endpoint that does not require prior authentication.
Request Body
The user’s registered email address
The user’s password (will be compared against the bcrypt hash)
Authentication Flow
Verify Password
Compare the provided password against the stored bcrypt hash using
bcrypt.compare().Request Example
Response
Success Response (200 OK)
Success message confirming login
JWT token valid for 24 hours. Use this token in the Authorization header for protected endpoints.
The JWT token expires after 24 hours (1 day). Store this token securely and include it in the Authorization header for all protected API requests.
Error Responses
400 Bad Request - Missing Fields
400 Bad Request - Missing Fields
Returned when required fields are not provided.Cause: Email or password field is missing from the request body.
401 Unauthorized - Invalid Credentials
401 Unauthorized - Invalid Credentials
500 Internal Server Error
500 Internal Server Error
Returned when an unexpected server error occurs.Cause: Database connection issues or other server-side errors.
Implementation Details
The login endpoint is implemented in theauthController.js file:
The implementation is located at
src/controllers/authController.js:41 in the source repository.JWT Token Details
The generated JWT token contains the following payload:Token Signing Process
User’s unique identifier from the database
User’s email address
Token issued at timestamp (automatically added by jwt.sign)
Token expiration timestamp (automatically calculated as iat + 1 day)
Using the JWT Token
After successful login, include the token in all subsequent API requests:Password Verification
The login process uses bcrypt to securely compare passwords:- Secure comparison: Uses the salt embedded in the stored hash
- Timing-safe: Resistant to timing attacks
- No decryption needed: One-way comparison without revealing the original password
Token Expiration Handling
When a token expires after 24 hours:Token Expiry Detection
Protected endpoints will return a 403 error with message “Token inválido o expirado”.
User Re-authentication
The client application should detect this error and redirect the user to the login page.
Security Considerations
Bcrypt Password Verification
Bcrypt Password Verification
Passwords are never decrypted. Instead, bcrypt hashes the provided password and compares it with the stored hash.
JWT Secret Key
JWT Secret Key
The
JWT_SECRET environment variable must be kept secure and never exposed in client code or version control.Generic Error Messages
Generic Error Messages
The API returns the same error message (“Credenciales inválidas”) for both non-existent emails and wrong passwords to prevent email enumeration attacks.
HTTPS Required
HTTPS Required
Always use HTTPS in production to encrypt credentials during transmission.
Next Steps
Authentication Overview
Learn more about the JWT authentication system
Register New Users
Create new user accounts