Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rack Session Cookie and Sinatra - setting and accessing data

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

like image 734
nictrix Avatar asked Mar 03 '11 02:03

nictrix


People also ask

What is rack session cookie?

Session::CookieA session is able to store the same information that would ordinarily be sent via a post request in a Session Cookie. This is known as session based cookie management and Rack has different ways to implement cookies.

What are sessions Sinatra?

A session is a way for a web application to set a cookie that persists an identifier across multiple HTTP requests, and then relate these requests to each other: If you've signed in before the web application will be able to know that you're still the same user you've identified as a couple requests earlier.

What are cookies and sessions used for?

HTTP cookies, or internet cookies, are built specifically for Internet web browsers to track, personalize, and save information about each user's session. A “session” just refers to the time you spend on a site. Cookies are created to identify you when you visit a new website.

How do cookies work in Rails?

Cookies are read and written through ActionController#cookies. The cookies being read are the ones received along with the request, the cookies being written will be sent out with the response. Reading a cookie does not get the cookie object itself back, just the value it holds.


1 Answers

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...

like image 84
nictrix Avatar answered Oct 10 '22 11:10

nictrix