Art*_*kel 42 session ruby-on-rails devise
我对设计gem配置设置感到困惑:
# 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
Run Code Online (Sandbox Code Playgroud)
我想让用户选中"记住我"复选框(即让我保持登录状态),但默认会话超时为10分钟.10分钟后,它要求我再次登录,即使我点击了"记住我".如果这是真的那么remember_for真的没有意义.显然我在这里遗漏了一些东西.
dou*_*asr 28
Ryan是正确的,因为默认的Devise gem不支持:rememberable和:timeoutable选项.然而,像所有Ruby一样,如果你不喜欢其他编码器做出的决定,特别是当它偏离大多数用户可能期望的标准时,那么你可以简单地覆盖它.
感谢(拒绝)拉取请求,我们可以通过将以下代码添加到Devise配置文件(/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
Run Code Online (Sandbox Code Playgroud)
现在,您可以配置这两个选项并让它们按预期工作.
config.remember_for = 2.weeks
config.timeout_in = 30.minutes
Run Code Online (Sandbox Code Playgroud)
Rya*_*igg 17
它timeout_in
会在不活动的10分钟内自动退出,并且与remember_me
复选框不兼容.你可以有一个,但不能两个.
之前答案中的信息已过时.我测试过我的项目,它采用Rails 4
和Devise 3.5.1
和还检查色器件代码以确保万无一失.
现在看看是否Remember Me
选中了复选框:
如果yes
,它检查if remember_exists_and_not_expired
,所以基本上config.remember_for
用于会话管理
如果no
,它检查if last_access <= timeout_in.ago
,config.timeout_in
相应地使用
归档时间: |
|
查看次数: |
22166 次 |
最近记录: |