Skip to content

Accessing the authenticated JWT token

Edit this page

If you need to get the information of JWT token from a Controller or Service for some purposes, you can:

  1. Inject TokenStorageInterface and JWTTokenManagerInterface:

    1
    2
    3
    4
    5
    6
    7
    8
    use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
    use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
    
    public function __construct(TokenStorageInterface $tokenStorageInterface, JWTTokenManagerInterface $jwtManager)
    {
        $this->jwtManager = $jwtManager;
        $this->tokenStorageInterface = $tokenStorageInterface;
    }
  2. Call decode() in jwtManager, and getToken() in TokenStorageInterface.

    1
    $decodedJwtToken = $this->jwtManager->decode($this->tokenStorageInterface->getToken());

This returns the decoded information of the JWT token sent in the current request.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version