1. Go to this page and download the library: Download viktorf/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/ */
viktorf / jwt example snippets
$secret = 'some jwt secret';
$token = 'some jwt token';
$service = new \JWT\Service\JWTService($secret);
// Callback is not mandatory, you can just skip it in authenticate() call
$callback = function (JWT\Service\JWTHeader $header, JWT\Service\JWTPayload $payload)
{
if (empty($payload->customField)) {
throw new \JWT\Exception\JWTException("JWT token is invalid: \$payload->customField is needed");
}
log("JWT custom field retrieved: " . $payload->customField);
};
try {
$this->jwtService->authenticate($token, $callback);
} catch (JWT\Exception\JWTException $exception) {
throw new HttpException('Access denied', 403, $exception);
} catch (Throwable $error) {
throw new HttpException('Internal server error', 500, $error);
}
class ExtraJWTPayload extends \JWT\Service\JWTPayload
{
public string $customField = '';
public function isValid() : bool
{
if (empty($this->customField)) {
return false;
}
return true;
}
}
class ExtraJWTService extends \JWT\Service\JWTService
{
protected function getPayloadClass() : string
{
return ExtraJWTPayload::class;
}
}