Rack Session Cookie and Sinatra - setting and accessing data

こ雲淡風輕ζ 提交于 2019-12-12 08:35:37

问题


I was using Rack Session Pool, however my users would get kicked off one webserver thread onto another making the session data expire. I started toying around with just enable :sessions in Sinatra, however I am unable to use that because I have mutliple apps using Sinatra (same key it appears to be using - not sure if this is because its the same host or not)

So since my apps would break each other, I now am trying Rack Session Cookie and setting the variables (same thing as enable :sessions, but you can set the variables)

Great so that works! But now I cannot access the session data the way I was using it, in Rack Session Pool and in enable: sessions

session[:user] = nick
puts session[:user]

you get the idea...

Question is why can I access session data with session[:user] in Pool and Sinatra enable :sessions, but not in Rack Session Cookie? Am I missing anything? All I am doing is below

config.ru

  use Rack::Session::Cookie, :key => 'key',
                             :domain => "localhost",
                             :path => '/',
                             :expire_after => 14400, # In seconds
                             :secret => 'secret'

EDIT:

Did some more testing and found that it's actually putting it in the session variable, however as soon as it moves to a new method or redirection the session variable appears to be dropped (is this cookie really larger than 4KBs?!) - it can't be because enable :sessions works just fine


回答1:


Here's what I did to fix this problem:

  use Rack::Session::Cookie, :key => 'my_app_key',
                             :path => '/',
                             :expire_after => 14400, # In seconds
                             :secret => 'secret_stuff'

Do you see the difference from the above? - No Domain, if I let Rack::Session::Cookie specify the domain or the browser (whoever does it), I have no errors between mutliple Sinatra/Rack apps...




回答2:


Problem is with the domain 'localhost'. This thread describes in more details as to why having localhost as the domain wouldn't work: Cookies on localhost with explicit domain

A fix would be to setup a domain in your hosts file like

127.0.0.1    superduper.dev

Then set your domain in your sessions settings to superduper.dev. Then during development you can go to whatever port you might need. Ex. superduper.dev:5000



来源:https://stackoverflow.com/questions/5175854/rack-session-cookie-and-sinatra-setting-and-accessing-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!