2

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.

3
  • 2
    Dude. You're logged out now. How do you want to have user again from session? I see in your question some logical paradox. Commented Nov 29, 2012 at 8:54
  • I don't want to get the User, I want to be able to call the getUser() method without having a 500 internal error. When an unregistered user is using the website, I can call the getUser() because getToken() actually returns a valid token. After I set the token to NULL when programmatically logging the user off, my code is broken. Commented Nov 30, 2012 at 0:12
  • Why not do whatever it is you want to do with the user and then at the end of your function you kill the token/session to log them out? Commented Dec 6, 2012 at 13:10

1 Answer 1

1

Try

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

Now its security.token_storage if anyone's interested.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.