Authentication
MaqAgr API uses JSON Web Tokens (JWT) for secure authentication. All protected endpoints require a valid Bearer token in the request header.Authentication Flow
The authentication process follows these steps:Include Token in Requests
Send the token in the
Authorization header with the Bearer scheme for all protected endpoints.Registration
Create a new user account to receive an authentication token.Endpoint
Request Body
Full name of the user
Valid email address (must be unique)
Strong password meeting security requirements:
- Minimum 8 characters
- At least one uppercase letter
- At least one lowercase letter
- At least one number
- At least one special character
Example Request
Response (201 Created)
New users are automatically assigned
role_id: 2 (regular user). Only administrators can modify user roles.Login
Authenticate with existing credentials to receive a new token.Endpoint
Request Body
Registered email address
User’s password
Example Request
Response (200 OK)
Successful login updates the user’s
last_session field in the database to track activity.JWT Token Structure
Token Format
JWT tokens consist of three Base64-encoded parts separated by dots:Token Payload
The decoded payload contains:Unique user identifier
User’s email address (stored in lowercase)
User role:
1 = Admin, 2 = Regular UserUser’s full name
Issued at timestamp (Unix epoch)
Expiration timestamp (Unix epoch, 24 hours after
iat)Token Configuration
- Algorithm: HS256 (HMAC with SHA-256)
- Expiration: 24 hours (configurable via
JWT_EXPIRES_INenv variable) - Secret: Defined in
JWT_SECRETenvironment variable
Using Bearer Tokens
All protected endpoints require the token in theAuthorization header using the Bearer scheme.
Header Format
Example: Get User Profile
Response (200 OK)
Token Validation Middleware
The API uses theverifyTokenMiddleware (defined in src/middleware/auth.middleware.js) to validate tokens:
req.user for use in route handlers.
Protected Endpoints
The following endpoints require authentication:Authentication Endpoints
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
POST | /api/auth/register | Register new user | No |
POST | /api/auth/login | Login and get token | No |
POST | /api/auth/logout | Logout (stateless) | Yes |
GET | /api/auth/profile | Get user profile | Yes |
PUT | /api/auth/profile | Update profile | Yes |
PUT | /api/auth/password | Change password | Yes |
Resource Endpoints
All endpoints under these paths require authentication:/api/tractors- Tractor management/api/implements- Implement management/api/terrains- Terrain management/api/calculations- Power calculations/api/recommendations- Tractor recommendations/api/admin- Admin operations (requiresrole_id: 1)
Role-Based Access Control
Some endpoints require specific roles:Admin Middleware
TheisAdmin middleware restricts access to administrators only:
Role IDs
role_id: 1- Administrator (full access)role_id: 2- Regular User (standard access)
Admin-only endpoints include creating tractors (
POST /api/tractors), managing users, and other administrative functions.Error Responses
Missing Token
Invalid Token Format
Expired or Invalid Token
Tokens expire after 24 hours. When your token expires, simply log in again to receive a fresh token.
Insufficient Permissions
Invalid Credentials (Login)
Inactive User
Rate Limiting
Authentication endpoints have rate limiting to prevent abuse:- Login endpoint (
/api/auth/login): Stricter limits vialoginLimiter - Register endpoint (
/api/auth/register): Public limits viapublicLimiter - Protected endpoints: General API limits via
apiLimiter
Security Best Practices
Use HTTPS
Always use HTTPS in production to encrypt tokens in transit
Secure Storage
Store tokens securely (e.g., httpOnly cookies, secure storage)
Token Expiration
Tokens expire after 24 hours - implement refresh logic in your client
Strong Secrets
Use a cryptographically strong
JWT_SECRET (minimum 32 characters)Password Hashing
Passwords are hashed with bcrypt (10 salt rounds) before storage
Input Sanitization
All inputs are sanitized via
sanitizeInputs middleware (XSS protection)Logout
Since JWT is stateless, logout is handled client-side.Endpoint
Example Request
Response (200 OK)
The logout endpoint returns a confirmation message, but since JWT is stateless, you must delete the token on the client side to complete logout.
Password Management
Change Password
Authenticated users can change their password:The new password must meet the same security requirements as registration. The current password is verified before allowing the change.
Next Steps
API Reference
Explore all available API endpoints
Error Handling
Learn about error responses and handling
Rate Limiting
Understand API rate limits and quotas
Examples
View practical authentication examples
