<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
use Symfony\Component\Security\Core\Security;
class EventSubscriber implements EventSubscriberInterface
{
/**
* @var Security
*/
private $security;
private $tokenStorage;
public function __construct(Security $security, TokenStorageInterface $tokenStorage)
{
$this->security = $security;
$this->tokenStorage = $tokenStorage;
}
public function onKernelRequest(RequestEvent $event): void
{
try {
// trigger only for logged in users
if ($this->security->isGranted('IS_AUTHENTICATED_FULLY')) {
$token = $this->security->getToken();
/**
* @var User $user
*/
$user = $token->getUser();
if ($user != null && !$user->getIsConnected()) {
// $this->tokenStorage->setToken(null);
// $event->getRequest()->getSession()->clear();
}
}
} catch (AuthenticationCredentialsNotFoundException $e) {
// don't do anything here... or do whatever you want.
}
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
];
}
}