<?php
/*
* Friendu_Frontend // LoginListener.php
*
* (c) 2018 Carsten Zeidler
*/
namespace App\EventListener;
/**
* Description of LoginListener
*
* @author Carsten
*/
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use App\Entity\User;
class LoginListener implements EventSubscriberInterface {
private $em;
public function __construct(EntityManagerInterface $em) {
$this->em = $em;
}
public function onLoginSuccess(InteractiveLoginEvent $event) {
// Get the User entity.
$user = $event->getAuthenticationToken()->getUser();
// Update your field here.
$user->setLastLogin(new \DateTime());
// Persist the data to database.
$this->em->persist($user);
$this->em->flush();
}
public function onLoginError(AuthenticationEvent $event) {
// Login error
echo 'Error';
}
public static function getSubscribedEvents() {
return [
//SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
LoginSuccessEvent::class => 'onLoginSuccess'
];
}
}