我的 RoR 站点上有基于设计的身份验证系统,我需要在一段时间不活动后自动注销用户。但我的网站上也有一些页面,这些页面创建后可以长时间打开(用户只会观看页面,其中信息由 ajax 更新),并且我不想在打开此页面时退出用户。
有人知道怎么做吗?或者如何告诉 Devise ajax 请求也是用户活动?
我的 RoR 站点上有基于设计的身份验证系统,我需要在一段时间不活动后自动注销用户。但我的网站上也有一些页面,这些页面创建后可以长时间打开(用户只会观看页面,其中信息由 ajax 更新),并且我不想在打开此页面时退出用户。
有人知道怎么做吗?或者如何告诉 Devise ajax 请求也是用户活动?
首先确保你已经安装了 devise 1.5.2,如果没有,升级它,这个问题已经 6 个月了 :-) 我希望你已经解决了这个问题。
当您位于要防止自动注销的页面时,您可以更改 timeout_in 返回值。
例如:
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
然后,您可以轻松地在 ApplicationController 添加一个 before_filter 来设置session[:auto_sign_out]
值
像这样:
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
此外,您可以添加其他条件以确保您正在检查您想要的页面,方法是检查页面的控制器名称,如下所示:
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
您需要检查页面控制器的名称,我希望这会有所帮助