2012-01-21 139 views
3

我不知道爲什麼,但是我的會話被刪除了每個請求。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 
+0

如果我簡化你的代碼並在這裏運行,它就可以工作。您可以提供的任何其他數據?抱歉問這個問題,但在瀏覽器中啓用了cookies *,對不對?你有沒有嘗試看一下HTTP頭文件? –

+1

正在重置的會話向我表明,這與[攻擊防護](http://www.sinatrarb.com/intro.html#Configuring%20attack%20protection)有關。嘗試添加'disable:protection'並查看是否改變了任何內容。 (我不會建議禁用保護,但它可以幫助您確定發生了什麼)。 – matt

+0

謝謝你們兩位,我剛剛檢查過,我正在運行Shotgun,所以服務器每次都重新啓動。 – toy

回答

7

爲了保持會話一致性,你需要設置一個會話密鑰,如:

set :session_secret, 'super secret' 

當它沒有設置西納特拉生成應用隨機的啓動和獵槍重新啓動在每次請求之前應用。

+1

這太棒了!它幫助了我! – Dmitry

+1

這是5小時調試的解決方案! –