Skip to content

Latest commit

 

History

History
60 lines (44 loc) · 1.99 KB

7-manual-token-creation.rst

File metadata and controls

60 lines (44 loc) · 1.99 KB

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:

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): JsonResponse
    {
        // ...

        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:

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\Security\Http\Authentication\AuthenticationSuccessHandler;

class FooController extends Controller
{
    public function fooAction(AuthenticationSuccessHandler $authenticationSuccessHandler): JsonResponse
    {
        $user = ...; // Fetch user

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

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

return $authenticationSuccessHandler->handleAuthenticationSuccess($user, $jwt);