如何防止在特定页面上自动注销用户?

时间:2022-05-11 23:12:37

I have devise-based auth system on my RoR-site, and I need to auto sign out user 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.

我的ror站点上有基于偏差的auth系统,我需要在不活动的一段时间后自动签出用户。但是我的站点上也有一些页面,这些页面被创建了很长时间(用户只需要看页面,那里的信息是通过ajax更新的),我想在打开这个页面时不签出用户。

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

有人知道怎么做吗?或者如何告诉设计ajax请求也是用户活动?

1 个解决方案

#1


1  

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.

首先确定你已经安装了1.5.2,如果不是,升级它,这是6个月的问题:-),我希望你已经解决了这个问题。

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

当您在您想要防止自动登录的页面时,您可以更改timeout_in返回值。

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

然后,您可以在ApplicationController中轻松添加before_filter以设置会话[:auto_sign_out]值。

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

您需要检查页面控制器的名称,希望这能有所帮助。

#1


1  

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.

首先确定你已经安装了1.5.2,如果不是,升级它,这是6个月的问题:-),我希望你已经解决了这个问题。

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

当您在您想要防止自动登录的页面时,您可以更改timeout_in返回值。

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

然后,您可以在ApplicationController中轻松添加before_filter以设置会话[:auto_sign_out]值。

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

您需要检查页面控制器的名称,希望这能有所帮助。