What are JWT Tokens?
JSON Web Tokens (JWT) are a secure way to transmit information between parties as a JSON object. In StockAPI, JWT tokens are used to authenticate users and maintain secure sessions. A JWT token contains encoded information about the user, including:- User ID
- Username
- User role
- Token expiration time
Token Structure
When you successfully log in, StockAPI generates a JWT token with the following payload:id- The user’s unique database IDusername- The user’s usernamerole- The user’s role (e.g., “vendedor”, “developer”)- Expiration set to 4 hours from generation
Obtaining a Token
To obtain a JWT token, you need to authenticate using the/login endpoint.
Login Request
Successful Response
If authentication is successful, you’ll receive a response like this:Error Responses
Usuario no encontrado (401)
Usuario no encontrado (401)
Contraseña incorrecta (401)
Contraseña incorrecta (401)
Error en el login (500)
Error en el login (500)
Using Tokens in Requests
Once you have a token, you must include it in theAuthorization header of all requests to protected endpoints.
Authorization Header Format
The token must be sent in the following format:Example Authenticated Requests
Token Verification Process
When you send a request with a token, theverifyToken middleware validates it:
- Checks if the
Authorizationheader is present - Extracts the token from the “Bearer” format
- Verifies the token signature and expiration
- Decodes the token and attaches user information to the request
- Allows the request to proceed or returns an error
Token Expiration
Tokens expire 4 hours after generation. After expiration, you must log in again to obtain a new token.
Handling Expired Tokens
When a token expires, requests will fail with a 403 status:- Make a new login request with valid credentials
- Obtain a new token
- Update your application’s stored token
- Retry the original request
Best Practices
Store Securely
Store tokens securely in your application (e.g., httpOnly cookies, secure storage)
Handle Expiration
Implement automatic token refresh logic before the 4-hour expiration
Don't Share Tokens
Never share tokens between users or expose them in client-side code
Clear on Logout
Remove stored tokens when users log out of your application
Common Authentication Errors
| Status Code | Message | Cause | Solution |
|---|---|---|---|
| 401 | No se proporcionó token | Missing Authorization header | Include the Authorization header in your request |
| 401 | Token inválido | Malformed token or missing Bearer prefix | Check the Authorization header format |
| 403 | Token expirado o inválido | Token has expired or signature is invalid | Log in again to obtain a new token |
| 401 | Usuario no encontrado | Invalid username during login | Verify the username is correct |
| 401 | Contraseña incorrecta | Invalid password during login | Verify the password is correct |
Next Steps
User Roles
Learn about role-based access control
Products API
Start making authenticated requests to manage products