views:

1089

answers:

2

I often want to clear the session store in Rails, in particular, the default cookie-based session store. Some sites seem to suggest that

rake tmp:sessions:clear

accomplishes this task, but it appears that it does not. What is the proper way to clear the cookie-based session store?

+1  A: 

It occurs to me now that what I want may not be possible depending on how the cookie-based store is implemented. If the cookies contain all the information the server needs (including a signature for data integrity) then the server does not need to store any information on its side therefore there is no way to invalidate existing cookies. I had assumed the cookie contained some key that corresponded to data on the server-side in order to verify that the cookie is valid, but now I realize this may not be the case.

If this is true, then the only way to clear cookies would be to change the server-side cookie secret used for signing and then presumably restart the server process.

+1  A: 

The problem is that cookies are client side. Running a rake task on your server won't delete cookies on all the machines that have visited the web page, obviously.

Perhaps you can use session.clear in your controllers somehow? You're right about changing the cookie key, though. Doing so would invalidate any session belonging to the old key. You would have to rescue from ActionController::StaleSession (or something like that), but it'd work.

August Lilleaas
Right, as I alluded to in my comment I was under the mistaken impression that the cookie keeps only up some sort of session token between the client and server and either the the token is also stored on the server-side in order to validate the client's token or the data is on the server-side.Temporarily I just have a workaround in my code to ignore the session. Perhaps the easiest thing is to switch to a databased-backed cookie store for the development environment (I want to clear the sessions just for development purposes).