Programmatically logout current user

和自甴很熟 提交于 2019-12-11 03:38:32

问题


I am trying to programmatically logout the current user from inside a listener. I read here that

$this->get('security.context')->setToken(null);
$this->get('request')->getSession()->invalidate();

does the trick but then I can't call $this->container->get('security.context')->getToken()->getUser(); anymore as the token is now NULL.

How can I log out the user but still let the application run normally? I have calls to getUser() in my controller functions so I should set back the token to something corresponding to an non authenticated user. How can I do this?

Also, if there is a way to programmatically start a new session and set a flash message to inform the user he has been logged out, it would be awesome.


回答1:


Try

$anonToken = new AnonymousToken('theTokensKey', 'anon.', array());
$this->get('security.context')->setToken($anonToken);

first parameter is the token's key (i.e. '50cdf89882454')



来源:https://stackoverflow.com/questions/13600280/programmatically-logout-current-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!