The Estructuras Backend API uses JSON Web Tokens (JWTs) to authenticate users across requests. When a user logs in successfully, the server signs a token containing the user’s ID, stores it in an HTTP-only cookie, and expects that cookie to be present on every subsequent request to a protected route. This approach keeps tokens out of JavaScript-accessible storage and relies on the browser’s built-in cookie security model.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/nelrondon/backend-proyecto-estructuras/llms.txt
Use this file to discover all available pages before exploring further.
Token Creation
ThecreateAccessToken function lives in src/libs/jwt.js and wraps jwt.sign in a Promise, making it compatible with async/await throughout the codebase.
- The
payloadis the data you want to embed in the token — typically{ id: user.id }. SECRET_KEYis loaded from environment variables viasrc/config.js.- Tokens expire after 7 days (
expiresIn: "7d"). After expiry the token is cryptographically invalid and the client must log in again. - The function returns a
Promise<string>, so callersawaitit and receive the signed token string.
SECRET_KEY must be a long, cryptographically random string — at least 32 random bytes encoded as hex or base64. Never commit it to source control. Set it via an environment variable in your deployment platform (e.g. a Vercel environment secret or a .env file that is listed in .gitignore).Token Verification
verifyToken mirrors createAccessToken by wrapping jwt.verify in a Promise. It decodes and validates a raw token string, resolving with the decoded payload or rejecting if the token is invalid or expired.
payload contains all fields that were passed to createAccessToken plus standard JWT claims such as iat (issued at) and exp (expiration timestamp).
Cookie Settings
After creating a token (typically in the login or register controller), the server sets it as an HTTP-only cookie:| Setting | Value | Purpose |
|---|---|---|
httpOnly | true | Prevents client-side JavaScript from reading the cookie, blocking XSS-based token theft |
secure | true | Ensures the cookie is only transmitted over HTTPS |
sameSite | "none" | Allows the cookie to be sent on cross-site requests (required because the frontend and backend are on different origins) |
maxAge | 604800000 ms | Cookie expires after 7 days, matching the JWT’s own expiry |
The authRequired Middleware
The authRequired middleware in src/middlewares/validateToken.js guards any route that requires an authenticated user. It reads the token cookie, verifies it using jwt.verify with a callback, and either attaches the decoded user to the request or terminates the request with an error response.
Response codes
| Scenario | HTTP Status | Body |
|---|---|---|
No token cookie present | 401 Unauthorized | { "message": "No Token, Unauthorized" } |
| Token present but invalid or expired | 403 Forbidden | { "message": "Invalid Token" } |
| Token valid | — | Calls next(), populates req.user |
Using req.user in protected handlers
Once authRequired calls next(), the decoded JWT payload is available as req.user in every downstream handler on that route. Because createAccessToken is always called with { id: user.id }, you can reliably read the authenticated user’s database ID from req.user.id: