PHP code example of josegarcia / jwt-auth

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

    

josegarcia / jwt-auth example snippets



Garcia\JWT;

$jwt = new JWT('secret');

$payload = [
    'user_id' => 1,
    'username' => 'johndoe',
    'email' => '[email protected]',
    'role' => 'admin'
];

$token = $jwt->encode($payload);

 echo "$token \n\n";
 
// $decoded = $jwt->decode($token);
// var_dump($decoded);

try {
    $decoded = $jwt->decode($token);
    var_dump($decoded);
} catch (\Exception $e) {
    echo "Token invalid: " . $e->getMessage();
}