A simple library to sign and verify JWT.

dev-main 2023-08-24 10:24 UTC

This package is auto-updated.

Last update: 2024-12-24 13:29:33 UTC


README

Note : This package uses HS256 algorithm only.

Usage

Requires a 256 bit secret key, needs to be generated manually.

$secret = 'x31thQ9QO0mZb1dKq6uejr9g7YE1uMwf';

Signing a token

$data = [
    'name' => 'John Doe',
    'exp' => '1655683200'
];

$token = JWT::sign($data, $secret);

exp claim in the payload should be an epoch timestamp.

Verifying a token

try {
    $payload = JWT::verify($token, $secret);
} catch (InvalidTokenException $e) {
    // ...
}

If the token is valid, payload of the token is returned.

InvalidTokenException is thrown if the token is invalid or expired.