2011-02-17 62 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相应