我希望在我的rails应用程序中会话到期后呈现登录页面。我没有使用任何gem进行身份验证。如果有任何帮助,我们将不胜感激。下面是我的应用程序控制器。
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_filter :session_expiry, :except => [:login]
def current_user
if session[:user_id].nil?
false
else
true
end
end
helper_method :current_user
def session_expiry
expire_time = session[:expires_at] || Time.now
@time_left = (expire_time - Time.now).to_i
unless @time_left > 0
reset_session
flash[:error] = 'Session Timeout!'
end
end
end发布于 2013-06-17 15:27:18
也许您必须在application_controller.js中定义另一个方法来检查会话是否过期,如果是,则重定向:
def redirect_to_login_if_session_expire
redirect_to(login_path) if session[:user_id].nil?
end其中:user_id可以是current_user.id
https://stackoverflow.com/questions/17140816
复制相似问题