![](/img/trans.png)
[英]Devise, how to set auto redirection to a specific page if the user already logged in?
[英]How to prevent Devise from auto logout user on specific page?
我在RoR站点上有基于devise的身份验证系统,并且一段时间不活动后需要自动注销用户。 但是我的网站上也有一些页面,这些页面被创建为可以长时间打开(用户将仅观看到页面,其中信息由ajax更新),并且我不想在打开此页面时退出用户。
有人知道该怎么做吗? 还是如何告诉Devise ajax请求也是用户活动?
首先,请确保您已经设计了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
您需要检查页面控制器的名称,希望对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.