Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
21.43% |
3 / 14 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ClearAuthenticationCookieOnLogoutListener | |
21.43% |
3 / 14 |
|
50.00% |
1 / 2 |
7.37 | |
0.00% |
0 / 1 |
| getSubscribedEvents | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| onLogout | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | namespace Apie\ApieBundle\EventListeners; |
| 3 | |
| 4 | use Apie\Common\Events\AddAuthenticationCookie; |
| 5 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 6 | use Symfony\Component\Security\Http\Event\LogoutEvent; |
| 7 | |
| 8 | /** |
| 9 | * Configuring a logout url in symfony does not guarantee that the cookie apie uses for authentication |
| 10 | * is removed, so you would stay logged in to apie even though symfony logged you out. This listener |
| 11 | * makes sure the authentication cookie is removed from the response whenever symfony logs a user out. |
| 12 | */ |
| 13 | class ClearAuthenticationCookieOnLogoutListener implements EventSubscriberInterface |
| 14 | { |
| 15 | public static function getSubscribedEvents(): array |
| 16 | { |
| 17 | // run after the logout success handler, so the response is already set. |
| 18 | return [ |
| 19 | LogoutEvent::class => ['onLogout', -100], |
| 20 | ]; |
| 21 | } |
| 22 | |
| 23 | public function onLogout(LogoutEvent $event): void |
| 24 | { |
| 25 | $response = $event->getResponse(); |
| 26 | if ($response === null) { |
| 27 | return; |
| 28 | } |
| 29 | $response->headers->clearCookie( |
| 30 | AddAuthenticationCookie::COOKIE_NAME, |
| 31 | '/', |
| 32 | null, |
| 33 | true, |
| 34 | true, |
| 35 | 'lax' |
| 36 | ); |
| 37 | } |
| 38 | } |