Setting session length with Devise

Ruby on-RailsDevise

Ruby on-Rails Problem Overview


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

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

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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:

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

You can now also set the timeout dynamically.

Solution 2 - Ruby on-Rails

With Rails4, the better thing to follow is:

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

class User < ActiveRecord::Base
  devise :timeoutable
end

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

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

Solution 3 - Ruby on-Rails

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

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjoshsView Question on Stackoverflow
Solution 1 - Ruby on-RailsBrian DeterlingView Answer on Stackoverflow
Solution 2 - Ruby on-RailsAnkushView Answer on Stackoverflow
Solution 3 - Ruby on-RailsrusllonrailsView Answer on Stackoverflow