I\'m looking for a way to log user out of Symfony 2 application, but could not find a way to do it properly.
I\'ve tried an approach described here: Symfony2: how to
@Slava Fomin II
Symfony already implements the functionality of logging user out and deleting cookies. There is a LogoutListener who delegates those action to couple of logout handlers: CookieClearingLogoutHandler and SessionLogoutHandler. I think the best course of action would be to call those handlers and not to implement such low-level logic yourself. However, I can't find a way to do this.
Why not simply create a service that calls those?
I looked into Symfony\Component\Security\Http\Firewall\LogoutListener and tested that he calls 2 services during logout (Symfony 3.2.9).
$tokenBasedRememberMeServices by the way deletes the remember-me cookie.
sessionLogoutHandler = $sessionLogoutHandler;
$this->tokenBasedRememberMeServices = $tokenBasedRememberMeServices;
$this->defaultLogoutSuccessHandler = $defaultLogoutSuccessHandler;
$this->tokenStorage = $tokenStorage;
}
public function logout(Request $request): void
{
$token = $this->tokenStorage->getToken();
$response = $this->defaultLogoutSuccessHandler->onLogoutSuccess($request);
$this->sessionLogoutHandler->logout($request, $response, $token);
$this->tokenBasedRememberMeServices->logout($request, $response, $token);
}
}