2011-02-17 72 views
40

我很困惑與色器件寶石配置設置:設計記住和會話

# The time the user will be remembered without asking for credentials again. 
    config.remember_for = 2.weeks 

    # The time you want to timeout the user session without activity. After this 
    # time the user will be asked for credentials again. 
    config.timeout_in = 10.minutes 

我想有一個用戶選擇「記住我」複選框(即,讓我登錄),但默認會話超時是10分鐘。 10分鐘後,它要求我再次登錄,即使我點擊了「記住我」。如果這是真的,那麼remember_for是沒有意義的。很明顯,我在這裏錯過了一些東西。

+3

我不認爲你應該在同一時間使用這兩種配置。 – Rafal 2011-02-17 21:28:35

回答

16

timeout_in將在您處於非活動狀態的10分鐘內自動註銷您,並且與remember_me複選框不兼容。你可以有一個,但不能兩個都有。

+2

不再有效,請看下面的pat的評論 – 2013-11-27 06:48:56

+1

remember_me無效,無論是啓用還是禁用超時。 – indb 2016-03-14 10:41:40

+1

在較新的設計版本中,兩個模塊都是兼容的,但是可記憶似乎優先於可超時。 – aromero 2016-06-08 17:31:53

27

瑞恩是正確的,因爲默認的Devise gem不支持:rememberable和:timeout兩個選項。然而,就像Ruby的所有事情一樣,如果你不喜歡其他編碼器做出的決定,特別是當它偏離大多數用戶可能期望的標準時,那麼你可以簡單地覆蓋它。

多虧了(拒絕)pull request我們可以通過添加以下代碼到你的設計配置文件的頂部(/config/initializers/devise.rb)覆蓋此行爲:

module Devise 
    module Models 
    module Timeoutable 
     # Checks whether the user session has expired based on configured time. 
     def timedout?(last_access) 
     return false if remember_exists_and_not_expired? 
     last_access && last_access <= self.class.timeout_in.ago 
     end 

     private 

     def remember_exists_and_not_expired? 
     return false unless respond_to?(:remember_expired?) 
     remember_created_at && !remember_expired? 
     end 
    end 
    end 
end 

現在,這將允許您配置這兩個選項並讓它們按預期工作。

config.remember_for = 2.weeks 
config.timeout_in = 30.minutes 
8

以前的答案中的信息已過時。我測試了我的項目,它使用Rails 4Devise 3.5.1also checked devise code以確保。

現在看起來Remember Me複選框是否被選中:

  • 如果yes,它會檢查if remember_exists_and_not_expired,所以基本上使用config.remember_for會話管理

  • 如果no,它會檢查if last_access <= timeout_in.ago,使用config.timeout_in相應