Symfony Guard or Userprovider – username always null

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 SecurityCoreUser object !
interesting isn’t it ?

    public function getUser($credentials, UserProviderInterface $userProvider)
    {
        return new SymfonyComponentSecurityCoreUserUser('nicolas','0000',['ROLE_API_USER']);
    }

dumped content

Source: Symfony4 Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *