fkoessler
fkoessler

Reputation: 7257

Programmatically logout current user

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.

Upvotes: 2

Views: 2371

Answers (1)

topaz1008
topaz1008

Reputation: 215

Try

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

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

Upvotes: 1

Related Questions