开发者

How to prevent Devise from auto logout user on specific page?

开发者 https://www.devze.com 2023-03-06 21:51 出处:网络
I have devise-based auth system on my RoR-site, and I need to auto sign out u开发者_StackOverflow中文版ser after some time of inactivity. But also I have some pages on my site, that created to be open

I have devise-based auth system on my RoR-site, and I need to auto sign out u开发者_StackOverflow中文版ser after some time of inactivity. But also I have some pages on my site, that created to be opened for long time (user will just watch to page, where info is updated by ajax) and I want to NOT sign out user when this page is opened.

Is anybody has a idea how to do that? Or how to tell Devise that ajax request is a user activity too?


First make sure you have devise 1.5.2 installed, if not, upgrade it, its been 6 months at this question :-) and I hope you already solved the problem.

You can change the timeout_in return value when you are at the page you want to prevent auto signout at.

For example:

class Student < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :timeoutable

  def timeout_in
      if session[:auto_sign_out] then
         #the normal period of signout
         30.minutes
      else
         #very long period, to prevent sign-out
         100.years
      end
  end
end

Then, you can easily add a before_filter at ApplicationController to set the session[:auto_sign_out] value

like this:

before_filter :manage_page_auto_sign_out


def manage_page_auto_sign_out
# check the params, and see if the id is the id of the specific page
if params[:id] == EXCLUDED_PAGE_ID then
session[:auto_sign_out]= false
else
session[:auto_sign_out]= true
end
end

additionally, you can add other conditions to make sure that you are checking against the page you want, by checking the controller name of page like this:

def manage_page_auto_sign_out
# check the params, and see if the id is the id of the specific page
if controller_name == 'pages' && params[:id] == EXCLUDED_PAGE_ID then
session[:auto_sign_out]= false
else
session[:auto_sign_out]= true
end
end

you need to check the name of the page controller, I hope this helps

0

精彩评论

暂无评论...
验证码 换一张
取 消