JWT
The Jwt class provides a simple interface for working with JSON Web Tokens (JWT) using the firebase/php-jwt library. It handles token generation and validation with support for standard JWT claims.
Environment Variables
# Required
JWT_SECRET=your-secret-key-here
JWT_ISSUER=https://your-domain.com
# Optional (defaults to HS256)
JWT_ALGORITHM=HS256Usage
Generating a Token
use Stilmark\Base\Jwt;
// Generate a token with custom claims
$token = Jwt::generate([
'user_id' => 123,
'email' => 'user@example.com',
// Add any custom claims here
]);
// With custom expiration (in seconds)
$token = Jwt::generate(
['user_id' => 123],
86400 // 24 hours
);Validating a Token
Using with AuthMiddleware
The AuthMiddleware automatically handles JWT validation from the Authorization header:
Security Considerations
Keep the JWT_SECRET secure - Never commit it to version control.
Use HTTPS - Always use HTTPS to prevent token interception.
Token Expiration - Always set a reasonable expiration time for tokens.
Sensitive Data - Avoid storing sensitive information in the token payload.
Last updated