Rails / Devise – 确定用户会话何时到期
当使用timeoutable
于Devise的timeoutable
模块时,如何确定当前用户会话到期之前的时间长度? 目标是在所有响应中包含此值,以便客户端脚本可以使用它。 我认为Devise在内部使用Warden进行身份validation,但我没有找到任何解释如何将会话到期时间拉出Warden深度的东西。
这是你如何做到的:
class User < ActiveRecord::Base devise :authenticatable, :timeoutable, :validatable, :timeout_in => 20.minutes end
看起来你可以像Benjamin Tan说的那样把它放在模型中,或者你可以输入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 = 30.minutes
请参阅此StackOverflow问题: 使用Devise设置会话长度 。
对于Rails 4:
配置/初始化/ devise.rb
if Rails.env.production? config.timeout_in = 30.minutes else config.timeout_in = 7.days end
user.rb
class User < ActiveRecord::Base devise :timeoutable end