38 lines
1.1 KiB
PHP
38 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\EventListener;
|
|
|
|
use App\Hydra\HydraService;
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
|
use Symfony\Component\Security\Http\Event\LogoutEvent;
|
|
|
|
class LogoutSubscriber implements EventSubscriberInterface
|
|
{
|
|
public function __construct(
|
|
private UrlGeneratorInterface $urlGenerator,
|
|
private HydraService $hydra
|
|
) {
|
|
}
|
|
|
|
public static function getSubscribedEvents(): array
|
|
{
|
|
return [LogoutEvent::class => 'onLogout'];
|
|
}
|
|
|
|
public function onLogout(LogoutEvent $event): void
|
|
{
|
|
// get the security token of the session that is about to be logged out
|
|
// get the current request
|
|
$request = $event->getRequest();
|
|
|
|
// get the current response, if it is already set by another listener
|
|
$request->getSession()->clear();
|
|
$request->getSession()->invalidate();
|
|
// configure a custom logout response to the homepage
|
|
$response = $this->hydra->handleLogoutRequest($request);
|
|
|
|
$event->setResponse($response);
|
|
}
|
|
}
|