src/EventListener/AuthenticationSuccessListener.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\User;
  4. use App\Manager\UserManager;
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
  7. class AuthenticationSuccessListener
  8. {
  9.     /** @var UserManager */
  10.     private $userManager;
  11.     public function __construct(UserManager $userManager)
  12.     {
  13.         $this->userManager $userManager;
  14.     }
  15.     /**
  16.      * @param AuthenticationSuccessEvent $event
  17.      */
  18.     public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event)
  19.     {
  20.         $data $event->getData();
  21.         /** @var User $user */
  22.         $user $event->getUser();
  23.         
  24.         if (!$user instanceof UserInterface) {
  25.             return;
  26.         }
  27.         // Save login datetime
  28.         $user->setLastLoginAt(new \DateTime());
  29.         $this->userManager->update($user);
  30.         
  31.         // Add data to JWT payload if wanted
  32.         // $data['user'] = array(
  33.         //     'id' => $user->getId(),
  34.         // );
  35.         
  36.         $event->setData($data);
  37.     }
  38. }