PHP code example of flytachi / jwt

1. Go to this page and download the library: Download flytachi/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/ */

    

flytachi / jwt example snippets


use Flytachi\Jwt\JWT;
use Flytachi\Jwt\Entity\JwtPayload;
use Flytachi\Jwt\Entity\PrivateKey;
use Flytachi\Jwt\Entity\PublicKey;

// encode
$jwtToken = JWT::encode(
    new JwtPayload([
        'iss' => 'https://domain.com',      // Issuer: who issued the token
        'sub' => 'user-12345',              // Subject: for whom the token is intended (user ID)
        'aud' => 'https://api.domain.com',  // Audience: what service is the token intended for?
        'iat' => time(),                    // Issued At: token issue time (Unix timestamp)
        'nbf' => time(),                    // Not Before: time from which the token becomes valid
        'exp' => time() + 3600,             // Expiration Time: time when the token expires (in 1 hour)
    ]),
    new PrivateKey('secret', 'HS256')
);

// decode
$payload = JWT::decode($jwtToken, [new PublicKey('secret', 'HS256')]);
$userId = $payload->getClaim('sub');