The Ecommerce Delivery API uses JSON Web Tokens (JWT) for authentication. After a user logs in, the server signs a token with theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/fredy-rizo/ecommerce-delivery/llms.txt
Use this file to discover all available pages before exploring further.
SECRET environment variable and returns it in the response. The client must include that token in the Authorization header for every request that requires authentication. Tokens last 365 days and carry the full user identity, so no session state is stored on the server.
The token is signed and verified using the
SECRET value set in your .env file. If SECRET is changed, all previously issued tokens become invalid immediately.Obtaining a token
Send aPOST request to /api/user/login with the user’s email and password.
200:
token string on the client. It is the only credential needed for subsequent calls.
Passing the token
Include the token in every authenticated request using the standardAuthorization header:
authorization key and splits on a space to extract the token portion (authHeader?.split(" ")[1]).
Example authenticated request:
Authentication middleware levels
There are three middleware functions insrc/middleware/libs/segurity.js. Routes stack them to enforce different access rules.
Token — required authentication
Applied to any route that needs a verified, logged-in user. The middleware:
- Reads the
Authorizationheader and extracts the Bearer token. - Verifies the token signature and expiry against
SECRET. - Looks up the user in MongoDB by the
_idembedded in the token payload. - Attaches a
req.userobject for downstream handlers.
req.user shape after Token succeeds:
TokenAdmin — admin-only access
Always chained after Token. It re-fetches the user from MongoDB and checks that at least one entry in roles has value === "2". If not, the request is rejected.
TokenOptional — optional authentication
Used on routes that serve both anonymous and authenticated users differently. If no token is present, req.user is set to null and the request continues. If a valid token is present, req.user is populated exactly as with Token. Any token error — including an expired token — sets req.user to false and the request continues rather than being rejected.
| Scenario | req.user value |
|---|---|
No Authorization header | null |
| Valid token | Full user object |
| Invalid, malformed, or expired token | false (request continues) |
| Valid token but user not found in DB | false (request continues) |
Token payload
The JWT payload is set at login time and includes:expiresIn: "365d", making it valid for one year from the moment of issue.
Error responses
| Status | Condition | Response body |
|---|---|---|
401 | No Authorization header or empty token | { "msj": "Sin autorizacion", "status": false } |
403 | Token signature is invalid or otherwise malformed | { "msj": "<error message>. Rechazo en la conexion", "status": false } |
403 | Token has expired | { "msj": "Sesion finalizada", "status": false } |
403 | User lacks the admin role (TokenAdmin check fails) | { "msj": "No tienes permisos para realizar esta accion", "status": false } |
404 | Token is valid but the user no longer exists in the database | { "msj": "Usuario no encontrado", "status": false } |
Middleware summary by route
| Route | Middleware applied |
|---|---|
POST /api/user/create | None (public) |
POST /api/user/verify-account | None (public) |
POST /api/user/resend-code | None (public) |
POST /api/user/login | None (public) |
POST /api/user/recover-password-code | None (public) |
POST /api/user/update-password-widthout-token | None (public) |
POST /api/user/update | Token |
POST /api/user/update-password-width-token | Token |
POST /api/user/list-members/:pag?/:perpage? | Token |
POST /api/user/update-role/:userId | Token → TokenAdmin |