Overview
ServITech uses JWT (JSON Web Tokens) for stateless authentication. Tokens are generated upon successful login and must be included in theAuthorization header for protected endpoints.
Token Generation
When you successfully authenticate via the/auth/login endpoint, the API returns:
The JWT access token for authentication
Token lifetime in seconds (default: 3600 seconds = 1 hour)
Token Structure
JWT tokens consist of three parts separated by dots:Header
Contains token type and signing algorithm:Payload
Contains user claims and metadata:Issuer - The API that issued the token
Issued At - Unix timestamp when token was created
Expiration - Unix timestamp when token expires
Not Before - Token is not valid before this timestamp
Subject - The user ID the token belongs to
JWT ID - Unique identifier for this token
Signature
Created using the header, payload, and secret key defined inJWT_SECRET environment variable.
Token Configuration
Configuration is managed inconfig/jwt.php:
| Setting | Environment Variable | Default | Description |
|---|---|---|---|
| Time to Live (TTL) | JWT_TTL | 60 minutes | Token expiration time |
| Refresh TTL | JWT_REFRESH_TTL | 20160 minutes (2 weeks) | Refresh window after expiration |
| Algorithm | JWT_ALGO | HS256 | Signing algorithm |
| Blacklist Enabled | JWT_BLACKLIST_ENABLED | true | Enable token blacklisting on logout |
| Leeway | JWT_LEEWAY | 0 seconds | Clock skew tolerance |
| Secret | JWT_SECRET | - | Secret key for signing (required) |
Using Tokens
Include the JWT token in theAuthorization header with the Bearer scheme:
Token Storage
Web Applications
For web applications, store tokens securely: Recommended approaches:-
HttpOnly Cookies (most secure)
- Not accessible via JavaScript
- Automatic inclusion in requests
- Protected from XSS attacks
-
Memory/State Management (e.g., Redux, Vuex)
- Token lost on page refresh
- Requires re-authentication
- Safe from XSS if properly implemented
- ❌ localStorage (vulnerable to XSS attacks)
- ❌ sessionStorage (vulnerable to XSS attacks)
Mobile Applications
For mobile apps:- iOS: Use Keychain Services
- Android: Use EncryptedSharedPreferences
- React Native: Use react-native-keychain or expo-secure-store
Token Expiration
Tokens expire after the configured TTL (default: 60 minutes).Handling Expired Tokens
When a token expires, the API returns:401 Unauthorized
Token Refresh
While the current implementation doesn’t expose a dedicated refresh endpoint, you can:- Re-authenticate - Use the
/auth/loginendpoint to get a new token - Implement refresh logic - Request a new token before expiration
The refresh window (
JWT_REFRESH_TTL) is 2 weeks by default. This setting is primarily for internal package behavior.Token Blacklisting
When a user logs out via/auth/logout, the token is added to a blacklist:
- Blacklist Enabled:
JWT_BLACKLIST_ENABLED=true(default) - Grace Period:
JWT_BLACKLIST_GRACE_PERIOD=0seconds (default)
Database Storage
Blacklisted tokens are stored in the database (via the configured storage provider) and checked on each authenticated request.Security Best Practices
Always use HTTPS
Always use HTTPS
Never transmit tokens over unencrypted HTTP connections. Always use HTTPS in production to prevent token interception.
Implement token expiration
Implement token expiration
Use reasonable TTL values (30-60 minutes recommended). Shorter lifetimes reduce the window for token abuse.
Validate tokens on every request
Validate tokens on every request
The API automatically validates tokens using the
auth:api middleware. Never skip this validation.Rotate secrets regularly
Rotate secrets regularly
Periodically rotate your
JWT_SECRET and invalidate all existing tokens by requiring re-authentication.Monitor for suspicious activity
Monitor for suspicious activity
Log authentication events and monitor for unusual patterns (e.g., tokens used from multiple IPs).
Error Handling
Common JWT-related errors:| Error | HTTP Status | Description | Solution |
|---|---|---|---|
| Token has expired | 401 | Token TTL exceeded | Re-authenticate to get a new token |
| Token invalid | 401 | Malformed or tampered token | Verify token format and signature |
| Token not found | 401 | No Authorization header | Include token in request header |
| Token blacklisted | 401 | Token was invalidated via logout | Re-authenticate to get a new token |
Related Resources
Login
Get a JWT token by logging in
Logout
Invalidate your token