1. Go to this page and download the library: Download daycry/jwt library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
daycry / jwt example snippets
use Daycry\JWT\JWT;
$jwt = JWT::for(); // pulls config('JWT')
// or inject an explicit config: new JWT(config('JWT'));
// Encode — the uid may be a string or an integer ID (e.g. a DB primary key)
$token = $jwt->encode(['user_id' => 42, 'role' => 'admin'], 'user-42');
// Decode + validate (throws on failure)
$claims = $jwt->decode($token); // Plain
echo $claims->claims()->get('uid'); // "user-42"
// Symmetric helper — get the original payload back
$payload = $jwt->getPayload($token); // ['user_id' => 42, 'role' => 'admin']
// Non-throwing alternative
$claims = $jwt->tryDecode($maybeBadToken);
if ($claims === null) {
return $this->response->setStatusCode(401);
}
public string $algorithm = \Lcobucci\JWT\Signer\Rsa\Sha256::class; // RS256
// or \Lcobucci\JWT\Signer\Ecdsa\Sha256::class for ES256
$jwt = JWT::for()->withLeeway(30); // accept up to ±30s of skew
$jwt = JWT::for()->withLeeway(null); // reset to no leeway
$jwt = JWT::for()
->withIssuer('https://api.my-app.com')
->withAudience('https://app-a.com', 'https://app-b.com') // multiple audiences
->withIdentifier(bin2hex(random_bytes(16))) // unique jti
->withClaims(['scope' => 'admin']) // extra top-level claims
->withHeader('x-trace', $traceId); // custom JOSE header
$claims = $jwt->getClaims($token); // validated array of all claims
$scope = $jwt->getClaim($token, 'scope'); // validated single claim
// Issuing side — stamp the active key id.
$token = JWT::for()->withKeyId('2026-06')->encode($data);
// Verifying side (app/Config/JWT.php) — accept old and new keys during the window.
public ?string $keyId = '2026-06';
public array $verifyingKeys = [
'2026-05' => '/path/old-public.pem',
'2026-06' => '/path/new-public.pem',
];
use Daycry\JWT\Exceptions\InvalidTokenException;
use Lcobucci\JWT\Validation\RequiredConstraintsViolated;
try {
$claims = $jwt->decode($token);
} catch (RequiredConstraintsViolated $e) {
// Signature, issuer, audience, exp, etc.
return $this->response->setStatusCode(401)->setJSON(['error' => $e->getMessage()]);
} catch (InvalidTokenException $e) {
// Malformed or non-Plain token.
return $this->response->setStatusCode(400)->setJSON(['error' => 'Bad token']);
}