0

Error

      Catchable Fatal Error: Argument 2 passed to Acme\StoreBundle\Security\TokenAuthenticator::__construct() must be an instance of Doctrine\Common\EventManager, instance of Doctrine\Bundle\MongoDBBundle\ManagerRegistry given, called in D:\xamppNew\htdocs\mtl_project\var\cache\dev\appDevDebugProjectContainer.php on line 6178 and defined

TokenAuthenticator.php

        <?php


        namespace Acme\StoreBundle\Security;

        use Doctrine\Common\EventManager;
        use Doctrine\Common\Persistence\ObjectManager;
        use Doctrine\Common\Persistence\ObjectRepository;
        use Doctrine\MongoDB\Connection;
        use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
        use Doctrine\ODM\MongoDB\Mapping\MappingException;
        use Doctrine\ODM\MongoDB\Hydrator\HydratorFactory;
        use Doctrine\ODM\MongoDB\Proxy\ProxyFactory;
        use Doctrine\ODM\MongoDB\Query\FilterCollection;
        use Doctrine\ODM\MongoDB\Repository\RepositoryFactory;

        //use Acme\StoreBundle\Security\TokenAuthenticator;
        use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;
        use Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor\AuthorizationHeaderTokenExtractor;
        use Symfony\Component\HttpFoundation\Request;
        use Symfony\Component\HttpFoundation\Response;
        use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
        use Symfony\Component\Security\Core\Exception\AuthenticationException;
        use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
        use Symfony\Component\Security\Core\User\UserInterface;
        use Symfony\Component\Security\Core\User\UserProviderInterface;
        use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;

        class TokenAuthenticator extends AbstractGuardAuthenticator
        {
        /**
        * @var JWTEncoderInterface
        */
        private $jwtEncoder;
        /**
        * @var Doctrine\Common\Persistence\ObjectManager
        */
        protected $om;
        /**
        * @param JWTEncoderInterface $jwtEncoder
        * @param ObjectManager       $om
        */
        public function __construct(JWTEncoderInterface $jwtEncoder, Doctrine\Common\Persistence\ObjectManager $om)
        {
        $this->jwtEncoder = $jwtEncoder;
        $this->om = $om;
        }
        /**
        * @inheritdoc
        */
        public function getCredentials(Request $request)
        {
        $extractor = new AuthorizationHeaderTokenExtractor(
        'Bearer',
        'Authorization'
        );
        $token = $extractor->extract($request);
        if (!$token) {
        return;
        }
        return $token;
        }
        /**
        * @inheritdoc
        */
        public function getUser($credentials, UserProviderInterface $userProvider)
        {
        $data = $this->jwtEncoder->decode($credentials);
        if ($data === false) {
        throw new CustomUserMessageAuthenticationException('Invalid Token');
        }
        $username = $data['username'];
        return $this->om
        ->getRepository('UserBundle:User')
        ->findOneBy(['username' => $username]);
        }
        /**
        * @inheritdoc
        */
        public function checkCredentials($credentials, UserInterface $user)
        {
        return true;
        }
        /**
        * @inheritdoc
        */
        public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
        {
        }
        /**
        * @inheritdoc
        */
        public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
        {
        }
        /**
        * @inheritdoc
        */
        public function supportsRememberMe()
        {
        return false;
        }
        /**
        * @inheritdoc
        */
        public function start(Request $request, AuthenticationException $authException = null)
        {
        return new Response('Token is missing!', Response::HTTP_UNAUTHORIZED);
        }
        }

Refrence

Difference between ObjectManager and EntityManager in Symfony2?

https://github.com/doctrine/mongodb-odm/blob/785c5039d51923d22902fa1249d1e3dd64018838/lib/Doctrine/ODM/MongoDB/DocumentManager.php#L44

  • im new in symfonymongodb bundle

  • can anyone suggest how can i use object manager in contructor as symfony is throwing errors.

afeef
  • 4,396
  • 11
  • 35
  • 65
  • You should add the service definition of the container here. The error seems a bit off, since the `Doctrine\Common\Persistence\ObjectManager` is required but `Doctrine\Common\EventManager` is expected. – Federkun Sep 07 '17 at 10:51
  • i tried code public function __construct(JWTEncoderInterface $jwtEncoder, Doctrine\Common\EventManager $om) { $this->jwtEncoder = $jwtEncoder; $this->om = $om; } Catchable Fatal Error: Argument 2 passed to Acme\StoreBundle\Security\TokenAuthenticator::__construct() must be an instance of Acme\StoreBundle\Security\Doctrine\Common\EventManager, instance of Doctrine\Bundle\MongoDBBundle\ManagerRegistry given, – afeef Sep 07 '17 at 10:58
  • ^ `You should add the service definition of the container here.` – Federkun Sep 07 '17 at 11:03

1 Answers1

1

doctrine_mongodb is a service that returns a Doctrine\Bundle\MongoDBBundle\ManagerRegistry object. You can get ObjectManager by calling getManager from it.

<?php


namespace Acme\StoreBundle\Security;

use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
// ...

class TokenAuthenticator extends AbstractGuardAuthenticator
{

    /**
    * @var Doctrine\Common\Persistence\ObjectManager
    */
    protected $om;

    // ...

    public function __construct(JWTEncoderInterface $jwtEncoder, ManagerRegistry $registry)
    {
        // ...
        $this->om = $registry->getManager();
    }
Federkun
  • 36,084
  • 8
  • 78
  • 90
  • hi fed now im having new problem Failed to load private key "D:\xamppNew\htdocs\mtl_project\app/../var/jwt/private.pem": ↵ 0906D06C:PEM routines:PEM_read_bio:no start lin – afeef Sep 07 '17 at 13:01
  • Open another question. How did you generate the key? Are you on windows? – Federkun Sep 07 '17 at 13:04
  • yup,i copy and pasted from url https://github.com/chalasr/lexik-jwt-authentication-sandbox/tree/master/var/jwt – afeef Sep 07 '17 at 13:07
  • 1
    you should generate it from your environment – Federkun Sep 07 '17 at 13:10
  • thanks fed now im able build app https://github.com/afeef1915/Angular2-Symfony3-Demo – afeef Sep 12 '17 at 05:51