PHP code example of dusta / jwt-ready

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

    

dusta / jwt-ready example snippets


use Dusta\JWTReady\JWTReady;

y' => 'YOUR-SECRET-KEY']);

// Return string
$jwt = $JWTReady->generate(['key' => 'value']);
// return eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJrZXkiOm51bGwsImlhdCI6bnVsbC.....

// if empty check headers Bearer/s
$check = $JWTReady->checkJWT($jwt);
// return ['jwt' => 'eyJ0eXAiOiJKV1QiLCJhbG....', 'payload' => [...]]

$decoded = $JWTReady->decode($jwt);
// return ['key' => 'value']

use Slim\Http\Request;
use Slim\Http\Response;

$app->get('/auth/check', function (Request $request, Response $response, array $args) {

    try {

        /** @var JWTReady $JWTReady */
        $JWTReady = $this->get('JWTReady');
        $checkJWT = $JWTReady->checkJWT();

    } catch (AuthorizationHeaderException $e) {
        return $response->withJson(['code' => 401, 'message' => 'Invalid BearerToken']);
    }

    return $response->withJson(['code' => 200])->withHeader('Bearer', $checkJWT->get('jwt'));
});



return [
    'settings' => [
        'displayErrorDetails' => true, // set to false in production
        'addContentLengthHeader' => false, // Allow the web server to send the content-length header

        'JWTReady' => [
            'key' => 'SecretKey',
            'iat' => null,
            'jti' => null,
            'iss' => null,
            'nbf' => null,
            'exp' => null,
            'data' => null
        ]
    ],
];

 php
use Dusta\JWTReady\JWTReady;

$container = $app->getContainer();

// monolog
$container['JWTReady'] = function ($c) {
    $settings = $c->get('settings')['JWTReady'];

    $JWTReady = new JWTReady($settings);
    return $JWTReady;
};