Symfony Guard or Userprovider - username always null

152 Views Asked by At

My Request goes through a ApiUserAuthenticator extends AbstractGuardAuthenticator to authenticate a request to an API. In the getUser() method I return an ApiUser.

    public function getUser($credentials, UserProviderInterface $userProvider)
    {
        return new ApiUser('jean', ['ROLE_API_USER'], '[email protected]');
    }

it works well because the user appears a little further in my controller.

    /**
     * @Route("/", name="API_INDEX")
     */
    public function index()
    {
        dump($this->getUser());
        return $this->json([
            'status' => true,
        ]);
    }

But a small detail is wrong, the username is missing ! It returns null.

dumped content

Of course in the simplistic ApiUser the constructor & getter are ok.
This curious phenomenon disappears when you send back a Security\Core\User object ! interesting isn't it ?

    public function getUser($credentials, UserProviderInterface $userProvider)
    {
        return new \Symfony\Component\Security\Core\User\User('nicolas','0000',['ROLE_API_USER']);
    }

dumped content

0

There are 0 best solutions below