Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getUser function in ControllerTrait not find service 'security.token_storage' with Symfony 6 and throws Exception #543

Open
iatanasov77 opened this issue Dec 22, 2022 · 1 comment

Comments

@iatanasov77
Copy link

iatanasov77 commented Dec 22, 2022

Description
Using the function getUser() from the ResourceController is not working in Symfony 6.

Steps to reproduce
Try to use getUser() in a controller and to view it.

Possible Solution
Use DI for the service 'security.token_storage' in the ResourceController

@kick-the-bucket
Copy link

You might be missing the security bundle, which should be indicated in the logs. This is what the getUser() method looks like:

protected function getUser()
    {
        if (!$this->container->has('security.token_storage')) {
            throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require symfony/security-bundle".');
        }

        if (null === $token = $this->container->get('security.token_storage')->getToken()) {
            return null;
        }

        if (!\is_object($user = $token->getUser())) {
            // e.g. anonymous authentication
            return null;
        }

        return $user;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants