Sinatra会话被自动销毁

toy*_*toy 3 ruby session facebook sinatra

我不确定为什么,但我的会话每次请求都被删除了.

这是我的代码

require 'rubygems'
require 'sinatra'
require 'sinatra/base'
require 'haml'
require 'facebook_oauth'
class MyClass < Sinatra::Base
  set :logging, true
  set :sessions, true
  get "/auth/facebook_callback" do

    // Do some facebook login which is fine    

    access_token = facebookClient.authorize(:code => params[:code])
    session[:access_token] = access_token.token
    session[:user] = facebookClient.me.info['name']
    session[:id] = facebookClient.me.info["id"]
    #print session by "pp session" I can still see all the sessions
    redirect '/'
  end

  get '/' do
    #print all the sessions again. And I can't see anything. The session_id is also different
  end
end
Run Code Online (Sandbox Code Playgroud)

les*_*est 7

要保持会话一致,您需要设置会话密钥,例如:

set :session_secret, 'super secret'
Run Code Online (Sandbox Code Playgroud)

如果没有设置,sinatra会在应用程序启动时生成随机数,并且在每次请求之前,shotgun会重新启动应用程序.