2

Error

 ServiceNotFoundException in CheckExceptionOnInvalidReferenceBehaviorPass.php 
 The service "token_authenticator" has a dependency on a non-existent service "lexik_jwt_authentication.jwt_encoder".

what i need

  • i need to validate angular2 app with symfony.

config.yml

lexik_jwt_authentication:
private_key_path: '%kernel.root_dir%/../var/jwt/private.pem'
public_key_path:  '%kernel.root_dir%/../var/jwt/public.pem'
pass_phrase:      '%jwt_key_pass_phrase%'
token_ttl: 3600

security.yml

 firewalls:
    main:
        pattern: ^/
        logout:       true
        anonymous:    true
        stateless: true

        guard:
            authenticators:
                - 'token_authenticator'

services.yml

 services:

 token_authenticator:
    class: AcmeStoreBundle\Security\TokenAuthenticator
    arguments: ['@lexik_jwt_authentication.jwt_encoder', '@doctrine_mongodb']

routing.yml

 acme_store_login_user:
   type: rest
   path:     /login_check
   defaults: { _controller: AcmeStoreBundle:Login:login }
name_prefix:  api_

Login controller code

  public function loginAction(Request $request) {

   $data = json_decode(file_get_contents('php://input'), true);
    $userName = $data['username'];
    $password = $data['password'];

    $user = $this->get('doctrine_mongodb')
            ->getRepository('AcmeStoreBundle:User')
            ->findOneBy(['username' => $userName]);

    if (!$user) {
        throw $this->createNotFoundException();
    }

    $isValid = $this->get('security.password_encoder')
            ->isPasswordValid($user, $password);

    if (!$isValid) {
        throw new BadCredentialsException();
    }

    $response = new Response(Response::HTTP_OK);
    $token = $this->getToken($user);

    $response = new Response($this->serialize(['token' => $token]), Response::HTTP_OK);
    return $this->setBaseHeaders($response);
}


public function serialize($data) {
    $context = new SerializationContext();
    $context->setSerializeNull(true);

    return $this->get('jms_serializer')
                    ->serialize($data, 'json', $context);
}

public function getToken(User $user) {

    return $this->container->get('lexik_jwt_authentication.encoder')
                    ->encode([
                        'username' => $user->getUsername(),
                        'exp' => time() + 3600 ,
    ]);
}

refrence:

https://github.com/chalasr/lexik-jwt-authentication-sandbox https://knpuniversity.com/screencast/symfony-rest4/create-json-web-token#play

  • can any one suggest how can i solve this problem.
afeef
  • 4,396
  • 11
  • 35
  • 65

2 Answers2

3

You're using the 2.x version, where (as you can see from the changelog) the lexik_jwt_authentication.jwt_encoder service (from the 1.x version) is no more. You should use lexik_jwt_authentication.encoder.default:

The service lexik_jwt_authentication.jwt_encoder has been removed in favor of lexik_jwt_authentication.encoder.default that supports OpenSSL and phpseclib crypto engines.

 token_authenticator:
    class: Acme\StoreBundle\Security\TokenAuthenticator
    arguments: ['@lexik_jwt_authentication.encoder.default', '@doctrine_mongodb']
Federkun
  • 36,084
  • 8
  • 78
  • 90
  • now im having new problem Attempted to load class "TokenAuthenticator" from namespace "AcmeStoreBundle\Security". Did you forget a "use" statement for another namespace? – afeef Sep 07 '17 at 09:43
  • post the `AcmeStoreBundle/Security/TokenAuthenticator.php` file – Federkun Sep 07 '17 at 09:46
  • i have upload full file url http://s000.tinyupload.com/?file_id=06840239099356572314 – afeef Sep 07 '17 at 09:49
  • you're using the wrong namespace. also, paste the code in your question. – Federkun Sep 07 '17 at 09:52
  • can you suggest in code snippet its about entity manager orm:mysql.but i need mongo db so tried like and use DoctrineMongoDB; public function __construct(JWTEncoderInterface $jwtEncoder, DoctrineMongoDB $em) { $this->jwtEncoder = $jwtEncoder; $this->em = $em; } – afeef Sep 07 '17 at 10:00
  • that's is another question that deserve another answer – Federkun Sep 07 '17 at 10:03
  • HI fed i have added new question https://stackoverflow.com/questions/46094093/how-to-use-object-manager-in-symfony3 regard object manager – afeef Sep 07 '17 at 10:30
  • Thanks mate ! It helped me – Faiyaz Md Abdul Sep 20 '17 at 07:14
0

I got the same problem with Symfony 4, because My LoginConroller extends BaseController, So make sure your LoginController extends the Controller Class . Like This :

class LoginController extends Controller {
  .......

}

gausoft
  • 91
  • 1
  • 8