1

I'm using Symfony 5.4, I use LexikJWTAuthenticationBundle to generate my tokens. For the /login_check part, everything is fine but I have a /me route to retrieve users via their token that doesn't work. My token is well in the header of the request but when I want to recover it via the token_storage no value is returned.

/**
 * @Get("/me")
 */
public function me(Request $request)
{
    $user = null;
    VarDumper::dump($this->container->get('security.token_storage')->getToken());
    exit();
    $user = $this->container->get('security.token_storage')->getToken()->getUser();

    return new JsonResponse(array("status" => 200, "message" => "User create", "body" => $user), 200);
}

Response

Here is my configuration for the authentification

class LoginFormAuthenticator extends AbstractLoginFormAuthenticator
{
    use TargetPathTrait;

    const LOGIN_ROUTE = 'app_login';

    private UrlGeneratorInterface $urlGenerator;

    public function __construct(UrlGeneratorInterface $urlGenerator)
    {
        $this->urlGenerator = $urlGenerator;
    }

    public function authenticate(Request $request): Passport
    {
        $email = $request->request->get('email', '');

        $request->getSession()->set(Security::LAST_USERNAME, $email);

        return new Passport(
            new UserBadge($email),
            new PasswordCredentials($request->request->get('password', '')),
            [
                new CsrfTokenBadge('authenticate', $request->request->get('_csrf_token')),
            ]
        );
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): Response
    {
        if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {
            return new RedirectResponse($targetPath);
        }

        // For example:
        //return new RedirectResponse($this->urlGenerator->generate('some_route'));
        throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);
    }

    protected function getLoginUrl(Request $request): string
    {
        return $this->urlGenerator->generate(self::LOGIN_ROUTE);
    }
}

Any idea ?

Vitalizzare
  • 4,496
  • 7
  • 13
  • 32
IDK
  • 11
  • 1

0 Answers0