Authentication overview
The E-Commerce API uses a dual authentication system to secure endpoints:- API Key Authentication - Required for all requests to verify the client application
- JWT Token Authentication - Required for user-specific operations like profile management, cart, and orders
API key authentication
All API requests must include a valid API key in the request headers. This verifies that the request is coming from an authorized client application.How it works
The API key is validated using middleware that checks thex-api-key header against the server’s configured key:
authMiddleware.mjs
The API key is stored in the server’s
.env file as API_KEY. Contact your API administrator to obtain the key for your environment.Using the API key
Include the API key in thex-api-key header with every request:
Endpoints requiring only API key
These public endpoints only require the API key header:POST /api/register- User registrationPOST /api/login- User loginGET /api/category- List all categoriesGET /api/product- Browse productsGET /api/product/:slug- Get product details
JWT token authentication
For user-specific operations, the API uses JSON Web Tokens (JWT) to authenticate requests. JWT tokens are issued when users register or log in, and must be included in subsequent requests to protected endpoints.How it works
JWT tokens are validated using middleware that extracts and verifies the token from the Authorization header:authMiddleware.mjs
req.user.
Obtaining a JWT token
You can obtain a JWT token through registration or login.Registration
Register a new user to receive a JWT token:curl
Login
Login with existing credentials to receive a JWT token:curl
Token expiration
JWT tokens are issued with an 8-hour expiration time:authController.mjs
Using the JWT token
Include the JWT token in theAuthorization header using the Bearer scheme:
Endpoints requiring JWT token
These protected endpoints require both the API key AND a valid JWT token:GET /api/user- Get current user profilePATCH /api/user- Update user name or emailPATCH /api/user/password- Update user passwordPOST /api/user/profile-photo- Upload profile photoPOST /api/cart- Add item to cartGET /api/cart- Get user’s cartDELETE /api/cart- Remove item from cartPOST /api/address- Add shipping addressGET /api/address- Get user’s addressesDELETE /api/address- Delete addressPATCH /api/address- Update addressPOST /api/review- Add product review
Admin authentication
Admin endpoints use a separate authentication key for management operations:curl
Authentication flow
Here’s a typical authentication flow for a new user:Security best practices
Store tokens securely
Never store JWT tokens in localStorage or sessionStorage where they’re vulnerable to XSS attacks. Use httpOnly cookies or secure storage mechanisms.
Use HTTPS in production
Always use HTTPS in production to encrypt tokens in transit and prevent man-in-the-middle attacks.
Implement token refresh
For production applications, implement a token refresh mechanism to avoid requiring users to re-login every 8 hours.
Validate on every request
The API validates tokens on every protected request, ensuring expired or invalid tokens are rejected immediately.