/**
  * {@inheritDoc}
  */
 public function onAuthenticationSuccess(Request $request, TokenInterface $token)
 {
     $user = $token->getUser();
     $jwt = $this->jwtManager->create($user);
     $response = new JsonResponse();
     $event = new AuthenticationSuccessEvent(array('token' => $jwt), $user, $request, $response);
     $this->dispatcher->dispatch(Events::AUTHENTICATION_SUCCESS, $event);
     $response->setData($event->getData());
     return $response;
 }
 /**
  * test identity field
  */
 public function testIdentityField()
 {
     $dispatcher = $this->getEventDispatcherMock();
     $dispatcher->expects($this->at(0))->method('dispatch')->with($this->equalTo(Events::JWT_CREATED), $this->isInstanceOf('Aegis\\Bundle\\JWTAuthenticationBundle\\Event\\JWTCreatedEvent'));
     $dispatcher->expects($this->at(1))->method('dispatch')->with($this->equalTo(Events::JWT_ENCODED), $this->isInstanceOf('Aegis\\Bundle\\JWTAuthenticationBundle\\Event\\JWTEncodedEvent'));
     $encoder = $this->getJWTEncoderMock();
     $encoder->expects($this->once())->method('encode')->willReturn('secrettoken');
     $manager = new JWTManager($encoder, $dispatcher, 3600);
     $manager->setUserIdentityField("email");
     $this->assertEquals('secrettoken', $manager->create(new CustomUser('user', 'password', '*****@*****.**')));
 }