src/EventSubscriber/LoginSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use App\Entity\Account
  7. class LoginSubscriber implements EventSubscriberInterface
  8. {
  9.     private EntityManagerInterface $em;
  10.     public function __construct(EntityManagerInterface $em)
  11.     {
  12.         $this->em $em;
  13.     }
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [
  17.             InteractiveLoginEvent::class => 'onLoginSuccess',
  18.         ];
  19.     }
  20.     public function onLoginSuccess(InteractiveLoginEvent $event): void
  21.     {
  22.         $user $event->getAuthenticationToken()->getUser();
  23.         if ($user instanceof Account) {
  24.             $user->setLastConnection(new \DateTimeImmutable());
  25.             $this->em->persist($user);
  26.             $this->em->flush();
  27.         }
  28.     }
  29. }