Skip to content

Commit 88fe387

Browse files
committed
Documented new logout event
1 parent 1e3df40 commit 88fe387

File tree

2 files changed

+113
-3
lines changed

2 files changed

+113
-3
lines changed

reference/configuration/security.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,9 +538,18 @@ The ``invalidate_session`` option allows to redefine this behavior. Set this
538538
option to ``false`` in every firewall and the user will only be logged out from
539539
the current firewall and not the other ones.
540540

541+
.. _reference-security-logout-success-handler:
542+
541543
success_handler
542544
~~~~~~~~~~~~~~~
543545

546+
.. deprecated:: 5.1
547+
548+
This option is deprecated since Symfony 5.1. Register an
549+
:doc:`event listener </event_dispatcher>` on the
550+
:class:`Symfony\\Component\\Security\\Http\\Event\\LogoutEvent`
551+
instead.
552+
544553
**type**: ``string`` **default**: ``'security.logout.success_handler'``
545554

546555
The service ID used for handling a successful logout. The service must implement

security.rst

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -899,11 +899,112 @@ Next, you'll need to create a route for this URL (but not a controller):
899899
And that's it! By sending a user to the ``app_logout`` route (i.e. to ``/logout``)
900900
Symfony will un-authenticate the current user and redirect them.
901901

902+
Customizing Logout
903+
~~~~~~~~~~~~~~~~~~
904+
905+
.. versionadded:: 5.1
906+
907+
The ``LogoutEvent`` was introduced in Symfony 5.1. Prior to this
908+
version, you had to use a
909+
:ref:`logout success handler <reference-security-logout-success-handler>`
910+
to customize the logout.
911+
912+
In some cases you need to execute extra logic upon logout (e.g. invalidate
913+
some tokens) or want to customize what happends after a logout. During
914+
logout, a :class:`Symfony\\Component\\Security\\Http\\Event\\LogoutEvent`
915+
is dispatched. Register an :doc:`event listener or subscriber </event_dispatcher>`
916+
to execute custom logic. The following information is available in the
917+
event class:
918+
919+
``getToken()``
920+
Returns the security token of the session that is about to be logged
921+
out.
922+
``getRequest()``
923+
Returns the current request.
924+
``getResponse()``
925+
Returns a response, if it is already set by a custom listener. Use
926+
``setResponse()`` to configure a custom logout response.
927+
928+
902929
.. tip::
903930

904-
Need more control of what happens after logout? Add a ``success_handler`` key
905-
under ``logout`` and point it to a service id of a class that implements
906-
:class:`Symfony\\Component\\Security\\Http\\Logout\\LogoutSuccessHandlerInterface`.
931+
Every Security firewall has its own event dispatcher
932+
(``security.event_dispatcher.FIREWALLNAME``). The logout event is
933+
dispatched on both the global and firewall dispatcher. You can register
934+
on the firewall dispatcher if you want your listener to only be
935+
executed for a specific firewall. For instance, if you have an ``api``
936+
and ``main`` firewall, use this configuration to register only on the
937+
logout event in the ``main`` firewall:
938+
939+
.. configuration-block::
940+
941+
.. code-block:: yaml
942+
943+
# config/services.yaml
944+
services:
945+
# ...
946+
947+
App\EventListener\CutomLogoutListener:
948+
tags:
949+
- name: kernel.event_listener,
950+
event: 'Symfony\Component\Security\Http\Event\LogoutEvent'
951+
dispatcher: security.event_dispatcher.main
952+
953+
App\EventListener\CustomLogoutSubscriber:
954+
tags:
955+
- name: kernel.event_subscriber
956+
dispacher: security.event_dispatcher.main
957+
958+
.. code-block:: xml
959+
960+
<!-- config/services.xml -->
961+
<?xml version="1.0" encoding="UTF-8" ?>
962+
<container xmlns="http://symfony.com/schema/dic/services"
963+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
964+
xsi:schemaLocation="http://symfony.com/schema/dic/services
965+
https://symfony.com/schema/dic/services/services-1.0.xsd">
966+
967+
<services>
968+
<!-- ... -->
969+
970+
<service id="App\EventListener\CutomLogoutListener">
971+
<tag name="kernel.event_listener"
972+
event="Symfony\Component\Security\Http\Event\LogoutEvent"
973+
dispatcher="security.event_dispatcher.main"
974+
/>
975+
</service>
976+
977+
<service id="App\EventListener\CustomLogoutSubscriber">
978+
<tag name="kernel.event_subscriber"
979+
dispacher="security.event_dispatcher.main"
980+
/>
981+
</service>
982+
</services>
983+
</container>
984+
985+
.. code-block:: php
986+
987+
// config/services.php
988+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
989+
990+
use App\EventListener\CutomLogoutListener;
991+
use App\EventListener\CutomLogoutSubscriber;
992+
use Symfony\Component\Security\Http\Event\LogoutEvent;
993+
994+
return function(ContainerConfigurator $configurator) {
995+
$services = $configurator->services();
996+
997+
$services->set(CustomLogoutListener::class)
998+
->tag('kernel.event_listener', [
999+
'event' => LogoutEvent::class,
1000+
'dispatcher' => 'security.event_dispatcher.main',
1001+
]);
1002+
1003+
$services->set(CustomLogoutSubscriber::class)
1004+
->tag('kernel.event_subscriber', [
1005+
'dispatcher' => 'security.event_dispatcher.main',
1006+
]);
1007+
};
9071008
9081009
.. _security-role-hierarchy:
9091010

0 commit comments

Comments
 (0)