How to Enable Sessions with Sinatra

I just started using sessions with my Sinatra App and I came across a few different ways to implement cookies.

  • enable :sessions
  • use Rack::Session::Cookie
  • use Rack::Session::Pool

What does it mean to use sessions?

HTML is a stateless protocol meaning you can’t maintain data in variables between route requests. Each time you request a new route, all of the data in your application refreshes.

But in order to maintain some data you can use cookies. A cookie is a piece of text, no bigger than 4 kilobytes, which is stored on the user’s computer by a web server via a web browser. It is a key-value pair structure. The use of Cookies complements the stateless nature of the HTTP protocol as it provides enough memory to store pieces of information during HTTP transactions.

You can use cookie-based sessions in the Sinatra web framework. Here are a few different ways to do it.

1) enable :sessions

You can enable sessions with Sinatra and then use the session hash between routes and views. Enabling sessions actually stores all data in a cookie.

Here’s an example of how to enable sessions and then use the stored values across different routes:

require 'sinatra/base'

class SinatraApp < Sinatra::Base

enable :sessions

get '/welcome' do
  name = session[:name]
  "Hello, #{name}!"
end

post '/:name' do
  session[:name] = params[:name]
  redirect '/welcome'
end
end

2) use Rack::Session::Cookie

If you need to set additional parameters for sessions, like expiration date, you should use Rack::Session::Cookie directly instead of enable :sessions. Here is an example of some other parameters you might want to set.

require 'sinatra/base'

class SinatraApp < Sinatra::Base

use Rack::Session::Cookie, :key => 'rack.session',
                           :domain => 'foo.com',
                           :path => '/',
                           :expire_after => 2592000, # In seconds
                           :secret => 'some_secret'

get '/welcome' do
  name = session[:name]
  "Hello, #{name}!"
end

post '/:name' do
  session[:name] = params[:name]
  redirect '/welcome'
end
end

3) use Rack::Session::Pool

When you use Cookie (with the above two examples), you store all the session key:value pairs directly within the cookie.

An alternative is to use Pool, which only stores IDs in the cookie, and maintains the rest of the session hash within an instance variable that is created for you called @pool. This has a couple advantages: 1) faster and 2) you can keep any objects with it. However, when you restart your app all of the sessions are lost (which is not the case when you use Cookies).

For my application, I wanted to save objects across routes which is why I used Rack::Session::Pool.

require 'sinatra/base'
require 'customer.rb'

class SinatraApp < Sinatra::Base

use Rack::Session::Pool

get '/welcome' do
  name = session[:customer].name
  "Hello, #{name}!"
end

post '/:name' do
  session[:customer] = Customer.new(params[:name])
  redirect '/welcome'
end
end

Leave a Reply

Your email address will not be published. Required fields are marked *