<?php
namespace App\EventListener;
use App\Entity\User;
use App\Manager\UserManager;
use Symfony\Component\Security\Core\User\UserInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
class AuthenticationSuccessListener
{
/** @var UserManager */
private $userManager;
public function __construct(UserManager $userManager)
{
$this->userManager = $userManager;
}
/**
* @param AuthenticationSuccessEvent $event
*/
public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event)
{
$data = $event->getData();
/** @var User $user */
$user = $event->getUser();
if (!$user instanceof UserInterface) {
return;
}
// Save login datetime
$user->setLastLoginAt(new \DateTime());
$this->userManager->update($user);
// Add data to JWT payload if wanted
// $data['user'] = array(
// 'id' => $user->getId(),
// );
$event->setData($data);
}
}