Sinatra Sessions Not Persisting as Expected
It seems as though the session hash is not loaded until a session variable is referenced. So, for example, you get the expected result if you change the redirect handler to:
get '/redir' do
puts 'session2'
puts session[:foo]
pp session
'hello world'
end
I guess Sinatra is using the session directly from Rack. A quick peek at the source shows that the session hash is lazily loaded when the [] method (and others) are invoked:https://github.com/rack/rack/blob/master/lib/rack/session/abstract/id.rb
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. Why cookie doesn't work using Sinatra and Omniauth?
Is was a trivial problem, just add the path to the cookie: response.set_cookie 'test', {:value=> "facebook_callback", :path => "/"}
The reason why I did not notice was that there is a redirect to "/", so Chrome was only showing me the cookies for the path "/". Removing the redirect, I notice I have two cookies named "test". One with "/" path and the other with "/auth" path.
Related Topics
How to Use (Ruby) Rgeo to Transform (Unproject) Coordinates
Rails: How Does "New" Action Called "Create" Action
Sqlite3::Sqlexception: Duplicate Column Name While Migrating
Sidekiq to Execute at Specific Time in Every Timezones
Segmentation Fault with Ruby 1.8.7
I Am Getting This Gem Install Error for Kgio Gem When I Do a Bundle Install
Automatically Adding Proxy to All Http Connections in Ruby
Get Route for Base Class of Sti Class in Rails
Ruby on Rails Tutorial Section 3.2 Rspec Testing Error
How to Use Omniauth to Make Authenticated Calls to Services
Script Executes Successfully in Commandline But Not as a Cronjob
Completely Random Identifier of a Given Length
Ruby How to Generate a Tree Structure Form Array
Rails How to Tell If a Sidekiq Worker Is Done with Perform_Async
How to Compare Xml Output in a Cucumber Step Using a Multiline String Example