Ruby-on-rails 使用 Devise 设置会话长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4867298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:16:17  来源:igfitidea点击:

Setting session length with Devise

ruby-on-railsdevise

提问by joshs

My sessions with devise timeout after 1-3 hours of non-use (not sure exactly how long). How can I adjust this?

我的会话在 1-3 小时未使用后超时(不确定确切多长时间)。我该如何调整?

I've looked over the docs and can't seem to find a setting for this.

我查看了文档,似乎无法找到此设置。

Thanks

谢谢

回答by Brian Deterling

Look in config/initializers/devise.rb. There are a lot of configuration settings including config.timeout_in. The default in my version is 30 minutes. You can also set it on the model itself:

查看 config/initializers/devise.rb。有很多配置设置,包括config.timeout_in. 我的版本中的默认值为 30 分钟。您还可以在模型本身上设置它:

class User < ActiveRecord::Base
  devise :timeoutable, :timeout_in => 15.minutes

You can now also set the timeout dynamically.

您现在还可以动态设置超时

回答by Ankush

With Rails4, the better thing to follow is:

使用 Rails4,更好的做法是:

In models/user.rb: Add :timeoutableto already existing list of devise modules.

在 models/user.rb 中:将:timeoutable添加到现有的设计模块列表中。

class User < ActiveRecord::Base
  devise :timeoutable
end

In config/initializers/devise.rb: Set the timeout parameter.

在 config/initializers/devise.rb 中:设置超时参数。

Devise.setup do |config|
  config.timeout_in = 3.hours
end

回答by rusllonrails

Global:

全球的:

class User < ActiveRecord::Base
  devise (...), :timeoutable
end

Devise.setup do |config|
  config.timeout_in = 3.hours
end

Also it's possible to set timeout_in option dynamically

也可以动态设置 timeout_in 选项

class User < ActiveRecord::Base
  devise (...), :timeoutable

  def timeout_in
    if self.admin? 
      1.year
    else
      2.days
    end
  end
end