The Estructuras Backend API uses JSON Web Tokens (JWT) to authenticate users. When a user registers or logs in successfully, the server signs a JWT and delivers it to the client exclusively through an HTTP-only cookie namedDocumentation 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. Because the cookie is never exposed to JavaScript, it is immune to XSS-based token theft. All subsequent requests to protected routes automatically carry the cookie, and the authRequired middleware silently validates it on the server before any handler runs.
How the Token Is Issued
On a successfulPOST /api/register or POST /api/login, the server calls createAccessToken({ id: user.id }), which signs a JWT with the application’s SECRET_KEY and a 7-day expiry. The resulting token is written into the response cookie with the following settings:
| Cookie attribute | Value | Purpose |
|---|---|---|
httpOnly | true | Prevents JavaScript from reading the cookie |
secure | true | Transmits the cookie over HTTPS only |
sameSite | 'none' | Allows cross-site requests (required for separate front-end/back-end deployments) |
maxAge | 604 800 000 ms (7 days) | Defines the cookie lifetime |
Because
sameSite is set to 'none', the secure flag is mandatory — browsers will reject a sameSite: none cookie that is not also marked secure.The authRequired Middleware
Protected routes are guarded by the authRequired middleware defined in src/middlewares/validateToken.js. On every request it:
- Reads
req.cookies.token. - Returns 401 with
{ message: "No Token, Unauthorized" }if no cookie is present. - Calls
jwt.verify(token, SECRET_KEY)— returns 403 with{ message: "Invalid Token" }if the token is expired or tampered with. - Attaches the decoded payload (which contains
id) toreq.userand callsnext()on success.
Auth Endpoints
| Method | Path | Protected | Description |
|---|---|---|---|
POST | /api/register | No | Create a new user account |
POST | /api/login | No | Authenticate and receive a token cookie |
POST | /api/logout | No | Clear the token cookie and end the session |
GET | /api/ | Yes | Fetch the currently authenticated user’s profile |
GET | /api/verify-token | Yes | Validate the active session and return user data |
Public vs. Protected Routes
Public routes —/api/register, /api/login, and /api/logout — do not require a token. Any client can call them freely.
Protected routes — GET /api/ and GET /api/verify-token — are wrapped by the authRequired middleware. A request that arrives without a valid token cookie is rejected before it ever reaches the controller.
Sending the Cookie from a Client
Browser (Fetch API)
Cross-origin browser requests must includecredentials: 'include' so the browser attaches the cookie automatically:
curl
Use-c to save cookies to a file after login and -b to re-send them on subsequent requests:
Auth Pages
Register
Create a new user account with
POST /api/register.Login
Authenticate with username and password via
POST /api/login.Logout
Clear the session cookie with
POST /api/logout.Verify Token
Validate an active session with
GET /api/verify-token.