Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/floriansalvi/HEIG-VD_Ocha-api/llms.txt

Use this file to discover all available pages before exploring further.

The Ocha API uses a two-role permission model. Every user is assigned either the user role or the admin role at registration time, and that role is stored on their document in the database. Access to each endpoint is enforced by one or two middleware layers — protect (authentication check) and admin (role check) — that run before the request reaches the controller.

Roles

RoleAssigned by defaultDescription
userYesStandard authenticated access. Can place and manage own orders.
adminNoFull access including product, store, and order management.
The role is set at registration and is stored directly on the user document. There is no self-service role upgrade; roles must be assigned at account creation or updated directly in the database by a database administrator.

How authentication is enforced

Requests to protected endpoints must include a valid JWT in the Authorization header:
Authorization: Bearer YOUR_JWT_TOKEN
The protect middleware verifies the token, decodes the user ID, fetches the user from the database (excluding the password), and attaches it to req.user. If the token is missing, invalid, or expired, the middleware returns 401 before the request reaches the controller. The admin middleware runs after protect on admin-only endpoints. It checks that req.user.role === 'admin'. If the user is authenticated but not an admin, it returns 403.

Understanding 401 vs 403

StatusMeaning
401 UnauthorizedNo token was provided, or the token is invalid or expired. The request is not authenticated.
403 ForbiddenThe token is valid and the user is authenticated, but the user does not have the admin role.
Receiving a 403 on an admin endpoint does not mean your token is broken. It means your account’s role is user, not admin. Verify the role field on your user profile.

Endpoint permissions

Public endpoints — no authentication required

These endpoints are accessible to any client without a token:
MethodEndpointDescription
GET/api/v1/productsList all products
GET/api/v1/products/:idGet a product by ID
GET/api/v1/storesList all stores
GET/api/v1/stores/:idGet a store by ID

User endpoints — authentication required (user or admin)

These endpoints require a valid Bearer token. Both user and admin roles are accepted.
MethodEndpointDescription
POST/api/v1/ordersCreate a new order
GET/api/v1/orders/:idGet a specific order
DELETE/api/v1/orders/:idDelete a specific order
GET/api/v1/orders/:id/itemsGet items for a specific order
GET/api/v1/usersGet the authenticated user’s profile
GET/api/v1/users/ordersGet the authenticated user’s order history

Admin endpoints — admin role required

These endpoints require a valid Bearer token and the admin role. A user-role token returns 403.
MethodEndpointDescription
POST/api/v1/productsCreate a new product
PATCH/api/v1/products/:idUpdate a product
DELETE/api/v1/products/:idDelete a product
PUT/api/v1/products/:id/imageUpload or replace a product image
POST/api/v1/storesCreate a new store
PATCH/api/v1/stores/:idUpdate a store
DELETE/api/v1/stores/:idDelete a store
PATCH/api/v1/orders/:idUpdate an order’s status
GET/api/v1/order-statsGet aggregate order statistics

Middleware implementation summary

The protect middleware (middleware/authMiddleware.js) handles 401 responses for:
  • Missing Authorization header or token
  • Token that fails jwt.verify() (invalid signature, malformed, expired)
  • Token that decodes to a user ID not found in the database
The admin middleware (middleware/adminMiddleware.js) handles 403 responses for:
  • Authenticated users whose role field is not 'admin'

Next steps

Authentication

Learn how to register, log in, and obtain a JWT token.

Image upload

Upload product images using an admin token.

Error codes

Full list of error responses returned by the API.

Quickstart

Make your first authenticated API request in minutes.

Build docs developers (and LLMs) love