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=HS256

Usage

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

  1. Keep the JWT_SECRET secure - Never commit it to version control.

  2. Use HTTPS - Always use HTTPS to prevent token interception.

  3. Token Expiration - Always set a reasonable expiration time for tokens.

  4. Sensitive Data - Avoid storing sensitive information in the token payload.

Last updated