Sinatra Clears Session on Post

Sinatra clears session on post

I was suffering from the same issue as you: sessions were being cleared on post.

I have no idea why this works, but this is my solution:

#enable :sessions
use Rack::Session::Cookie, :key => 'rack.session',
:path => '/',
:secret => 'your_secret'

I literally just replaced the enable :sessions bit with use Rack::Session::Cookie ... and now all is good in the world.

Deleting the current session with Rack::Session::Cookie

Just use

session.clear

to destroy the session.

Sinatra and session variables which are not being set

If you're using Shotgun, add the following line to the configure block:

set :session_secret, "My session secret"

To quote from rkh, Sinatra's current maintainer:

[Shotgun] will restart the server on every request, thereby regenerate the session secret and thus invalidate your sessions. This has been fixed in current master. Simple fix: set the session_secret option.

NOTE: This fix doesn't work if you use Rack::Session::Pool

Getting unique session ID in Sinatra

From what I can tell JSESSIONID is used to pass the session around in a query string, and Sinatra doesn't have something like that, at least not easily accessible. Sinatra uses Rack for session management, and by default uses a cookie to store all session data. There are other session options in Rack, like memcached, where a unique session id is stored in a cookie, but even there Rack abstracts that away so you don't ever need to see the session id (though it is still accessible, see the documentation).

If you want to go that route then look into messing with the Rack middleware in Sinatra, but if all you need is a unique id, then it would probably be easier to generate one yourself and store it in the session.



Related Topics



Leave a reply



Submit