2

The session is not preserved between requests, though I can't see what I'm doing wrong. Code!

require 'sinatra'
require 'rack/fiber_pool'

class SessionTest < Sinatra::Base
  use Rack::FiberPool
  enable :sessions
  set :session_secret, "foobar"

  get '/' do
        body { session.inspect } #This is always '{}'!
  end

  get '/a' do
    session['user'] = "bob"
    redirect '/'
  end
end

run SessionTest.new
B_.
  • 2,164
  • 2
  • 17
  • 20

2 Answers2

1

Try this instead:

require 'sinatra'
require 'rack/fiber_pool'

class SessionTest < Sinatra::Base
  enable :sessions
  set :session_secret, "foobar"

  get '/' do
        body { session.inspect } #This is always '{}'!
  end

  get '/a' do
    session['user'] = "bob"
    redirect '/'
  end
end

use Rack::FiberPool
run SessionTest.new

Otherwise Sinatra will set up the fiber pool after the session middleware, which doesn't work. This is not a bug but caused by the way Rack::FiberPool works.

Konstantin Haase
  • 25,687
  • 2
  • 57
  • 59
0

Turns out replacing enable :sessions with use Rack::Session::Cookie is enough to make it work.

But why!?

B_.
  • 2,164
  • 2
  • 17
  • 20