Log user out in Symfony 2 application when “remember me” is enabled

后端 未结 3 1372
孤街浪徒
孤街浪徒 2020-12-09 19:55

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

3条回答
  •  渐次进展
    2020-12-09 20:41

    @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);
        }
    
    }
    

提交回复
热议问题