Rails/Devise - Determining when user session will expire
Asked Answered
M

3

5

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.

Martinmas answered 22/5, 2013 at 0:54 Comment(0)
U
10

Here is how you do it:

class User < ActiveRecord::Base
  devise :authenticatable, :timeoutable, :validatable, :timeout_in => 20.minutes
end
Unruh answered 22/5, 2013 at 1:4 Comment(2)
I knew that, but it took your post to make me realize it was actually the answer. Thanks!Martinmas
What if I want 1 user to have different timeout and another user to different?Tortosa
A
8

Looks like you can put it in the model as Benjamin Tan says, or you can put in 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 StackOverflow question: Setting session length with Devise.

Appetite answered 9/10, 2013 at 23:14 Comment(0)
P
1

For Rails 4:

config/initializers/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
Phenosafranine answered 20/1, 2016 at 20:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.