JWT Configuration
JSON Web Token authentication reference.
Configuration
json
{
"jwt": {
"secret": "{{env.JWT_SECRET}}"
}
}JWT Payload
Standard claims:
json
{
"sub": "user-123",
"role": "admin",
"tenant_id": "tenant-456",
"exp": 1704067200,
"iat": 1704063600
}Generate Token
bash
npm run generate-jwtOr programmatically:
typescript
import jwt from 'jsonwebtoken';
const token = jwt.sign(
{
sub: 'user-123',
role: 'admin',
tenant_id: 'tenant-456'
},
process.env.JWT_SECRET,
{ expiresIn: '1h' }
);Protect Routes
json
{
"path": "/admin/users",
"method": "get",
"requireAuth": true
}Access Claims in Routes
json
{
"data": {
"user_id": "{{auth.sub}}",
"role": "{{auth.role}}",
"tenant_id": "{{auth.tenant_id}}"
}
}Request Header
bash
curl -X GET http://localhost:3000/api/protected \
-H "Authorization: Bearer <token>"Error Responses
Missing Token
json
{
"error": "Unauthorized",
"message": "No authorization token provided"
}Status: 401
Invalid Token
json
{
"error": "Unauthorized",
"message": "Invalid or expired token"
}Status: 401
Expired Token
json
{
"error": "Unauthorized",
"message": "Token expired"
}Status: 401
Best Practices
- Use minimum 32-character secrets
- Set appropriate expiration (1h recommended)
- Include only necessary claims
- Rotate secrets periodically
- Use HTTPS in production