Creating JWT tokens programmatically

Edit this page

Creating JWT tokens programmatically

It might be useful in many cases to manually create a JWT token for a given user, after confirming user registration by mail for instance. To achieve this, use the lexik_jwt_authentication.jwt_manager service directly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Core\User\UserInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;

class ApiController extends Controller
{
    public function getTokenUser(UserInterface $user, JWTTokenManagerInterface $JWTManager)
    {
        // ...

        return new JsonResponse(['token' => $JWTManager->create($user)]);
    }
}

This dispatches the Events::JWT_CREATED, Events::JWT_ENCODED events and returns a JWT token, but the Events::AUTHENTICATION_SUCCESS event is not dispatched, you need to create and format the response by yourself.

For manually authenticating an user and returning the same response as your login form:

1
2
3
4
5
6
public function fooAction(UserInterface $user)
{
    $authenticationSuccessHandler = $this->container->get('lexik_jwt_authentication.handler.authentication_success');

    return $authenticationSuccessHandler->handleAuthenticationSuccess($user);
}

You can also pass an existing JWT to the handleAuthenticationSuccess method:

1
2
3
$jwt = $this->container->get('lexik_jwt_authentication.jwt_manager')->create($user);

return $authenticationSuccessHandler->handleAuthenticationSuccess($user, $jwt);
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.