symfonysymfony-security

Symfony, force logout in a controller


I'm using Symfony 3.4, and I would like to logout my user at the end of my action in my controller.

This is the action

public function changeUserEmail() {
     /* change the user email */
     /* perform the logout */
     /* choose the route to redirect to */
     return $this->redirectToRoute(/* some route choosen above */);
}

Is there a way to implement /* perform the logout */ the Symfony way? I found nothing in the docs. I do want to logout in the controller (don't want to redirect to the logout path) and I want to choose the route to be redirected in the controller.

Many thanks.

Version or Symfony is 3.4


Solution

  • Here is the answer

    use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
    
    // ...
    
    public function changeUserEmail(TokenStorageInterface $tokenStorage) {
         /* change the user email */
         $tokenStorage->setToken();
         /* choose the route to redirect to */
         return $this->redirectToRoute(/* some route choosen above */);
    }
    

    There is no need to invalidate all the session, e.g. if one have multiple firewalls defined.