开发者

Sinatra session not preserved with Rack::FiberPool

开发者 https://www.devze.com 2023-03-09 15:04 出处:网络
The session is not preserved between requests, though I can\'t see what I\'m 开发者_Python百科doing wrong. Code!

The session is not preserved between requests, though I can't see what I'm 开发者_Python百科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


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.


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

But why!?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号