How JWT Authentication Works
The API implements a two-tier routing system with JWT middleware protection:- Public Routes - No authentication required
- Secured Routes - Require valid JWT token in Authorization header
/secured/* are protected by JWT middleware that validates tokens before allowing access to the endpoint.
JWT Middleware Implementation
The authentication middleware is implemented inroutes/setup/routes_setup.js:9-27:
routes/setup/routes_setup.js
- Extracts the token from the
Authorizationheader - Verifies the token against the JWT secret
- Returns 401 if token is missing, invalid, or expired
- Allows the request to proceed if token is valid
Login Process
Authentication Endpoint
POST/login
Authenticate a user and receive a JWT token.
Request Format
Authentication Flow
The login process is implemented inroutes/setup/servicios/cliente.js:54-90:
JWT token generation
If authentication succeeds, a JWT token is generated with 1-hour expiration:
routes/setup/servicios/cliente.js:68-70
Response Examples
Token Format and Expiration
Token Structure
JWT tokens consist of three parts separated by dots:- Header - Algorithm and token type
- Payload - User data (userId, nombre)
- Signature - Verification signature using JWT_SECRET
Token Payload
The token payload contains:userId- User’s email addressnome- User’s hashed password (used as additional identifier)iat- Issued at timestampexp- Expiration timestamp
Token Expiration
The expiration is configured incliente.js:69:
Using Tokens in Requests
Authorization Header Format
To access secured endpoints, include the JWT token in theAuthorization header of your requests:
Unlike Bearer token schemes, this API expects the raw token value directly in the Authorization header (without “Bearer” prefix).
Example: Get Products
Example: Update Product
Example: Upload to WooCommerce
Some endpoints like
post_products also require a user header containing the authenticated user’s email for audit logging purposes.Protected Endpoints
The following endpoints require JWT authentication (defined inroutes_setup.js:39-56):
Product Management
POST /secured/update_products- Update product informationPOST /secured/deleteProducto- Delete a productPOST /secured/add_producto- Add new productGET /secured/getProductos- List all productsGET /secured/getProductosPublicados- List published products
User Management
POST /secured/upload_user- Create new userPOST /secured/update_user- Update user informationPOST /secured/delete_user- Delete userGET /secured/getUsers- List all users
WooCommerce Integration
POST /secured/post_products- Publish products to WooCommerce
Audit Log
GET /secured/getBitacora- View activity log
Error Handling
Missing Token
Request without Authorization header:Authorization header is present (routes_setup.js:23-25).
Invalid Token
Request with malformed or tampered token:- Token signature is invalid
- Token format is malformed
- Token was signed with different JWT_SECRET
Expired Token
Request with expired token (>1 hour old):Security Best Practices
Keep JWT_SECRET Secure
The JWT_SECRET in
config/default.json is critical for token security. Never commit it to version control or expose it publicly.Use HTTPS in Production
Always use HTTPS in production to prevent token interception. Tokens sent over HTTP can be captured and reused.
Implement Token Refresh
Consider implementing a token refresh mechanism for better user experience, as tokens currently expire after 1 hour.
Store Tokens Securely
On the client side, store tokens securely (e.g., httpOnly cookies or secure storage). Avoid localStorage for sensitive applications.
Password Security
User passwords are secured using bcrypt hashing:During Registration
routes/setup/servicios/cliente.js:280
During Login
routes/setup/servicios/cliente.js:65
Complete Authentication Example
Here’s a complete workflow for authenticating and making requests:Next Steps
Now that you understand authentication, explore:- API endpoint documentation for available operations
- User management endpoints for creating and managing accounts
- WooCommerce integration for publishing products
API Reference
Explore all available endpoints
User Management
Learn about user roles and permissions