How can i force logout user logged from controle on the new Symfony 6 ? (Version 6.0.1)
I tried $tokenStorage->setToken($token); but setToken() need 2 args:
(public function setToken(string $tokenId, string $token);)
I tried $request->getSession()->invalidate(); but my user is always logged...
I want to logout the user and redirect to another route (à don't want redirect to logout route)
Thank you
I can't use /logout because i'm in a controller, and sometime I have to make sure no user is logged, because i do treatment when I'm comming to this route.
I need this:
When i go to /validate route:
- if user : logged => logout
- change somethings to my user, other user and flush some logs to bdd
- redirect to login page to force login back the user
My service:
<?php
namespace App\Service;
use Symfony\Component\Security\Http\Event\LogoutEvent;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class SecurityService
{
public function forceLogout(
Request $request,
EventDispatcherInterface $eventDispatcher,
TokenStorageInterface $tokenStorage) : void
{
$logoutEvent = new LogoutEvent($request, $tokenStorage->getToken());
$eventDispatcher->dispatch($logoutEvent);
$tokenStorage->setToken(null);
}
}
This don't work, my $eventDispatcher->dispacth($logoutEvent) work only before i refresh my page, after i'm logged again !
$tokenStorage->setToken(null);not sure if this has changed for Symfony 6. How about$id = $tokenStorage->getToken()->getId();then you can...->setToken($id, null);$tokenStorage->setToken(null)works fine on my end.Symfony\Component\Security\Http\Firewall\LogoutListener::authenticateand basically copy the relevant code. I know there was talk at one time of encapsulating this functionality into an official Logout service but I don't think that even happened. It is important you send the event even if things seem to work without it. You can easily run into hard to debug issues without it.