menu
Evenement lors de la connexion

Evenement lors de la connexion

Déclenche un evenement lorsqu'un utilisateur se connecte en créant un EventListener !

# config/services.yaml
App\EventListener\LoginListener:
      tags:
        - { name: 'kernel.event_listener', event: 'security.authentication.success' }


<?php
// src/EventListener/LoginListener.php
namespace App\EventListener;

use Doctrine\ORM\EntityManagerInterface;
use App\Entity\User;
use Symfony\Component\Security\Core\Event\AuthenticationEvent;

class LoginListener {
  private $em;

  public function __construct(EntityManagerInterface $em) {
    $this->em = $em;
  }

  public function onSecurityAuthenticationSuccess(AuthenticationEvent $event) {
    $user = $event->getAuthenticationToken()->getUser();

    if ($user instanceof User) {
      $user->setLastLogin(new \Datetime());

      $this->em->persist($user);
      $this->em->flush();
    }
  }
}