/** * test identity field */ public function testIdentityField() { $dispatcher = $this->getEventDispatcherMock(); $dispatcher->expects($this->once())->method('dispatch')->with($this->equalTo(Events::JWT_CREATED), $this->isInstanceOf('Lexik\\Bundle\\JWTAuthenticationBundle\\Event\\JWTCreatedEvent')); $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', '*****@*****.**'))); }
/** * {@inheritDoc} */ public function onAuthenticationSuccess(Request $request, TokenInterface $token) { $user = $token->getUser(); $jwt = $this->jwtManager->create($user); $response = new JsonResponse(); $event = new AuthenticationSuccessEvent(['token' => $jwt], $user, $request, $response); $this->dispatcher->dispatch(Events::AUTHENTICATION_SUCCESS, $event); $response->setData($event->getData()); return $response; }
/** * {@inheritdoc} * * Overrides event to add API token to the user's session. */ public function onAuthenticationSuccess(Request $request, TokenInterface $token) { /* * @var UserInterface Note this will return a ApiBundle\Entity\User instance; this dependence to ApiBundle * is due to the fact that this is the user class defined at the application * configuration level worry here. */ $user = $token->getUser(); $apiToken = $this->jwtManager->create($user); $request->getSession()->set('api_token', $apiToken); return parent::onAuthenticationSuccess($request, $token); }