Overview
SmartShelf API uses JSON Web Tokens (JWT) for secure, stateless authentication. Tokens are issued upon successful registration or login and must be included in subsequent requests to protected endpoints.Authentication Flow
Registration
Create a new user account.Endpoint
Request Body
User’s full name
Valid email address (stored in lowercase)
Password (minimum 6 characters)
User role:
Admin, Manager, or Worker (defaults to Worker)Example Request
Response
The token is also automatically set as an HttpOnly cookie named
token for browser-based clients.Login
Authenticate with existing credentials.Endpoint
Request Body
User’s email address
User’s password
Example Request
Response
Token Format and Usage
JWT Structure
Tokens are generated using thejsonwebtoken library with the following payload:
Token Expiration
- Default expiration: 7 days
- Configurable via:
JWT_EXPIREenvironment variable - Cookie expiration: Matches JWT expiration (configurable via
JWT_COOKIE_EXPIRE)
Authorization Header Format
Include the token in theAuthorization header of protected requests:
Example Authenticated Request
Cookie-Based Authentication
Alternatively, the API accepts tokens from HttpOnly cookies:The
protect middleware checks both the Authorization header and cookies (src/middlewares/authMiddleware.js:6-17).Protected Routes
All protected endpoints require a valid JWT token. The authentication middleware:- Extracts token from cookies or Authorization header
- Verifies token signature using
JWT_SECRET - Checks if user exists and is active
- Attaches user object to
req.user
Get Current User
Update Profile
Change Password
Logout
Role-Based Access Control (RBAC)
SmartShelf implements three user roles with different permission levels:- Admin
- Manager
- Worker
Full system access
- Manage all users
- Configure system settings
- Access all inventory and analytics
- Create/delete resources
Authorization Middleware
Theauthorize middleware restricts endpoints by role:
Example: Role Check Error
If a Worker attempts to access an Admin-only endpoint:Authentication Errors
Missing Token
Invalid or Expired Token
Deactivated Account
If the user’sisActive flag is false:
Invalid Credentials
Email Already Exists
Security Best Practices
Store tokens securely
Store tokens securely
- Use HttpOnly cookies in browser environments to prevent XSS attacks
- Store tokens in secure storage (not localStorage) on mobile/desktop apps
- Never expose tokens in URLs or logs
Handle token expiration
Handle token expiration
- Implement automatic logout on token expiration
- Consider implementing refresh token flow for long-lived sessions
- Display clear error messages when tokens expire
Use HTTPS in production
Use HTTPS in production
- Always use HTTPS to encrypt tokens in transit
- Set
secure: truein cookie options (controlled byNODE_ENV) - Configure proper CORS origins
Password requirements
Password requirements
- Minimum 6 characters (enforced by API)
- Passwords are hashed using bcryptjs with salt
- Never log or display passwords in plain text
Account security
Account security
- Change passwords regularly using
/api/auth/change-password - Monitor for unauthorized access attempts
- Admins can deactivate compromised accounts
Environment Variables
Required authentication configuration:Implementation Reference
Key files implementing authentication:- Controller:
src/controllers/authController.js- Handles registration, login, logout - Middleware:
src/middlewares/authMiddleware.js- Token verification and role authorization - Helper:
src/utils/jwtHelper.js- Token generation and verification - Model:
src/models/User.js- User schema with password hashing
Next Steps
API Overview
Learn about response formats and rate limiting
User Management
Explore user CRUD operations and permissions