PHP code example of baywa-re-lusy / jwt-authentication

1. Go to this page and download the library: Download baywa-re-lusy/jwt-authentication 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/ */

    

baywa-re-lusy / jwt-authentication example snippets


use BayWaReLusy\JwtAuthentication\TokenService;
use Laminas\Cache\Psr\CacheItemPool\CacheItemPoolDecorator;
use BayWaReLusy\JwtAuthentication\Token;

public function onAuthentication(MvcAuthEvent $e): IdentityInterface
{
    $jwt                          = ...; // The JSON Web Token
    $laminasCacheStorageInterface = ...; // Instance of Laminas\Cache\Storage\StorageInterface
    $jwksUrl                      = ...; // URL from where to get JWKs
    $cache                        = new CacheItemPoolDecorator($laminasCacheStorageInterface);
    
    $tokenService = new TokenService();
    
    try {
        $token = $tokenService->validateToken($jwt, $cache, $jwksUrl);
    } catch (\BayWaReLusy\JwtAuthentication\InvalidTokenException $e) {
        return new GuestIdentity();
    }
    
    ...
}