PHP code example of marvin255 / jwt-symfony

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

    

marvin255 / jwt-symfony example snippets


use Marvin255\Jwt\Symfony\Profile\JwtProfileManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use RuntimeException;

class SiteController extends AbstractController
{
    private JwtProfileManager $manager;

    public function __construct(JwtProfileManager $manager)
    {
        $this->manager = $manager;
    }

    public function read(Request $request): void
    {
        // select profile
        $jwtProfile = $this->jwtProfileManager->profile('basic');

        $tokenHeader = $request->headers->get('Authorization');

        // decode token from header string
        $token = $jwtProfile->getDecoder()->decodeString($tokenHeader);

        // validate token
        $validationResult = $jwtProfile->getValidator()->validate($token);
        if (!$validationResult->isValid()) {
            $message = implode('. ', $validationResult->getErrors());
            throw new RuntimeException($message);
        }
    }

    public function build(): void
    {
        // select profile
        $jwtProfile = $this->jwtProfileManager->profile('basic');

        // decode token from header string
        $token = $jwtProfile
            ->getBuilder()
            ->setJoseParam('test', 'test') // any custom JOSE param
            ->setIss('test')               // registered claims have own setters
            ->setClaim('test', 'test')     // any custom claim
            ->build();
    }
}