Rails / Devise - 确定用户会话何时到期

时间:2022-10-30 01:19:24

When using the timeoutable module for Devise, how can one determine how long it will be before the current user's session expires? The goal is to include this value in all responses so client side script can make use of it. I gather that Devise internally uses Warden for authentication, but I haven't found anything that explains how to pull the session expiration time out of the depths of Warden.

当使用适用于Devise的超时模块时,如何确定当前用户会话到期之前的时间长度?目标是在所有响应中包含此值,以便客户端脚本可以使用它。我认为Devise在内部使用Warden进行身份验证,但我没有找到任何解释如何将会话到期时间拉出Warden深度的东西。

3 个解决方案

#1


10  

Here is how you do it:

这是你如何做到的:

class User < ActiveRecord::Base
  devise :authenticatable, :timeoutable, :validatable, :timeout_in => 20.minutes
end

#2


7  

Looks like you can put it in the model as Benjamin Tan says, or you can put in config/initializers/devise.rb

看起来你可以像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

See this * question: Setting session length with Devise.

请参阅此*问题:使用Devise设置会话长度。

#3


0  

For Rails 4:

对于Rails 4:

config/initializers/devise.rb

配置/初始化/ devise.rb

  if Rails.env.production?
    config.timeout_in = 30.minutes
  else
    config.timeout_in = 7.days
  end

user.rb

user.rb

class User < ActiveRecord::Base
  devise :timeoutable
end

#1


10  

Here is how you do it:

这是你如何做到的:

class User < ActiveRecord::Base
  devise :authenticatable, :timeoutable, :validatable, :timeout_in => 20.minutes
end

#2


7  

Looks like you can put it in the model as Benjamin Tan says, or you can put in config/initializers/devise.rb

看起来你可以像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

See this * question: Setting session length with Devise.

请参阅此*问题:使用Devise设置会话长度。

#3


0  

For Rails 4:

对于Rails 4:

config/initializers/devise.rb

配置/初始化/ devise.rb

  if Rails.env.production?
    config.timeout_in = 30.minutes
  else
    config.timeout_in = 7.days
  end

user.rb

user.rb

class User < ActiveRecord::Base
  devise :timeoutable
end