2011-09-05 35 views

回答

4

我也在尋找這個功能,但沒有找到一種方法直接做到這一點。

可以重置每個登錄的認證令牌和使用rememberable在兩者之間:

在應用程序控制器

,在after_sign_in_path_for():在devise.rb

resource.reset_authentication_token 

config.remember_for = 1.day 

或者您可以創建一個cron-job來定期清除用戶表中無效的authentication_token條目。

+2

的感謝!我使用令牌作爲api_token。如果用戶從不同的設備/瀏覽器登錄會發生什麼情況?一個新的令牌將始終生成,並且唯一的一個設備將能夠與api進行通信(使用令牌)。我想要一個令牌生成一次,並可用,直到「註銷」被稱爲或超時通過。你知道關於api標記處理的任何「最佳實踐」嗎? – refaelos

+0

我瞭解你,我認爲認證令牌就像是用戶/密碼的替代品,通常用戶/密碼沒有過期時間(通常)。 – kambi

+0

明白了。謝謝 ! (戶田) – refaelos

2

我不確定這是不是你正在尋找,但這是一個簡單的選擇在設計。 如果設置在配置/初始化/ devise.rb

config.timeout_in下列選項= 30.minutes

然後設計應該閒置30分鐘後過期的令牌。 Devise爲會話身份驗證執行的相同操作也應該與身份驗證令牌一起使用。

我用,在我目前的項目,並測試它使用時空特警

it "should timeout without activity after 30 minutes" do 
    auth_token = @user.authentication_token 

    get "/frontend/users/#{@user.id}.json?auth_token=#{auth_token}" 
    response.status.should == 200 

    Timecop.travel(45.minutes.from_now) 
    get "/frontend/users/#{@user.id}.json?auth_token=#{auth_token}" 
    response.status.should == 401 

    Timecop.return 
end 

另外,我不認爲令牌遵循相同的比喻爲用戶名/密碼組合在評論中提及了,因爲你不會以純文本存儲你的密碼,但你使用你的令牌。我建議在每次註銷後重新設置令牌。

1

在色器件的初始化文件

#/config/initializers/devise.rb 

# ==> Configuration for :timeoutable 
# The time you want to timeout the user session without activity. After this 
# time the user will be asked for credentials again. Default is 30 minutes. 
config.timeout_in = 1.day 

# If true, expires auth token on session timeout. 
config.expire_auth_token_on_timeout = true