Skip to content

Adding full subscriber example #15186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 38 additions & 17 deletions security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1766,17 +1766,40 @@ In some cases you need to run extra logic upon logout (e.g. invalidate
some tokens) or want to customize what happens after a logout. During
logout, a :class:`Symfony\\Component\\Security\\Http\\Event\\LogoutEvent`
is dispatched. Register an :doc:`event listener or subscriber </event_dispatcher>`
to run custom logic. The following information is available in the
event class:

``getToken()``
Returns the security token of the session that is about to be logged
out.
``getRequest()``
Returns the current request.
``getResponse()``
Returns a response, if it is already set by a custom listener. Use
``setResponse()`` to configure a custom logout response.
to execute custom logic::

// src/EventListener/LogoutSubscriber.php
namespace App\EventListener;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Http\Event\LogoutEvent;

class LogoutSubscriber extends AbstractController implements EventSubscriberInterface
{
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
$token = $event->getToken();

// get the current request
$request = $event->getRequest();

// get the current response, if it is already set by another listener
$response = $event->getResponse();

// configure a custom logout response
$event->setResponse(
new RedirectResponse($this->generateUrl('homepage', []), RedirectResponse::HTTP_SEE_OTHER)
);
}
}

.. _retrieving-the-user-object:

Expand Down Expand Up @@ -2534,7 +2557,7 @@ for these events.
services:
# ...

App\EventListener\CustomLogoutSubscriber:
App\EventListener\LogoutSubscriber:
tags:
- name: kernel.event_subscriber
dispatcher: security.event_dispatcher.main
Expand All @@ -2551,7 +2574,7 @@ for these events.
<services>
<!-- ... -->

<service id="App\EventListener\CustomLogoutSubscriber">
<service id="App\EventListener\LogoutSubscriber">
<tag name="kernel.event_subscriber"
dispatcher="security.event_dispatcher.main"
/>
Expand All @@ -2564,14 +2587,12 @@ for these events.
// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use App\EventListener\CustomLogoutListener;
use App\EventListener\CustomLogoutSubscriber;
use Symfony\Component\Security\Http\Event\LogoutEvent;
use App\EventListener\LogoutSubscriber;

return function(ContainerConfigurator $configurator) {
$services = $configurator->services();

$services->set(CustomLogoutSubscriber::class)
$services->set(LogoutSubscriber::class)
->tag('kernel.event_subscriber', [
'dispatcher' => 'security.event_dispatcher.main',
]);
Expand Down