设计记住我和会话
我对 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
我想让用户选择“记住我”复选框(即让我保持登录状态),但默认会话超时为 10 分钟。 10 分钟后,即使我点击了“记住我”,它仍要求我再次登录。如果这是真的,那么 Remember_for 就真的毫无意义了。显然我在这里遗漏了一些东西。
I'm confused with the devise gem config settings:
# 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
I want to have a user select the "Remember Me" checkbox (i.e., keep me logged in), but the default session timeout is 10 minutes. After 10 minutes it asks me to log in again even though I have clicked "Remember me". If this is true then the remember_for is really meaningless. Obviously I'm missing something here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Ryan 是正确的,默认的 Devise gem 不支持 :rememberable 和 :timeoutable 选项。然而,与 Ruby 的所有事物一样,如果您不喜欢其他编码人员所做的决定,特别是当它偏离大多数用户可能期望的规范时,那么您可以简单地覆盖它。
感谢(被拒绝的)拉取请求,我们可以通过添加以下代码来覆盖此行为到您的 Devise 配置文件 (/config/initializers/devise.rb) 的顶部:
这将允许您配置这两个选项并让它们按您的预期工作。
Ryan is correct in that the default Devise gem does not support both the :rememberable and :timeoutable options. However, like all things Ruby, if you don't like the decision that some other coder has made, especially when it strays from the norm that most users are likely to expect, then you can simply override it.
Thanks to a (rejected) pull request we can override this behaviour by adding the following code to the top of your Devise config file (/config/initializers/devise.rb):
This will now allow you to configure both options and have them work as you would expect.
timeout_in
会在不活动的 10 分钟内自动注销您,并且与remember_me
复选框不兼容。您可以拥有其中之一,但不能同时拥有两者。The
timeout_in
will automatically log you out within 10 minutes of inactivity and is incompatible with theremember_me
checkbox. You can have one, but not both.以前的答案中的信息已过时。我已经测试了我的项目,该项目使用
Rails 4
和Devise 3.5.1
以及 还检查了设计代码以确定。现在它会检查
Remember Me
复选框是否被选中:if
yes
,它会检查if Remember_exists_and_not_expired
,所以基本上使用config.json。 Remember_for
用于会话管理如果
no
,它会检查if last_access <= timeout_in.ago
,相应地使用config.timeout_in
The information in previous answers is outdated. I've tested my project, which uses
Rails 4
andDevise 3.5.1
and also checked devise code to be sure.Now it looks whether
Remember Me
checkbox was checked:if
yes
, it checksif remember_exists_and_not_expired
, so basically usesconfig.remember_for
for session managementif
no
, it checksif last_access <= timeout_in.ago
, usingconfig.timeout_in
correspondingly