ruby on rails,会话过期通知 [英] ruby on rails, session expire notification

查看:39
本文介绍了ruby on rails,会话过期通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ruby​​ 1.9.3 和 rails 3.2.

Im Working with ruby 1.9.3 and rails 3.2.

我的实际会话处理如下:

My actual session handling looks like:

会话助手

    def sign_in(user)
            cookies[:remember_token] = {value: user.remember_token, expires: 20.minutes.from_now.utc}
            self.current_user = user
    end

    def sign_out
            self.current_user = nil
            cookies.delete(:remember_token)
    end

    def signed_in?
            if current_user.nil?
                    return false
            else
                    cookies[:remember_token] = {value: current_user.remember_token, expires: 20.minutes.from_now.utc}
            end

    def current_user=(user)
            @current_user = user
    end

    def current_user
            @current_user ||= User.find_by_remember_token(cookies[:remember_token])

    end

应用控制器

class ApplicationController < ActionController::Base
  before_filter :check_signin
  protect_from_forgery
  include SessionsHelper

  def check_signin
        unless signed_in?
        redirect_to signin_path
        end
  end
end

如果会话已过期,我想通知用户@登录页面.我如何检查会话是否已过期?请给出代码示例.

if the session is expired, i want to inform the user @ the sign in page. how can i check if the session is expired? Please give a code example.

推荐答案

由于您的 cookie 将在 20 分钟后过期,因此您无法区分新用户和会话已过期的用户.一个快速而肮脏的解决方案是测试谷歌分析 cookie 的存在,以了解用户的会话是否已过期.

As your cookie expires in 20 minutes, you can't differentiate a new user from a user with an expired session. A quick and dirty solution, would be to test the existence of a google analytics cookie to know if the user has an expired session.

def check_signin
  unless signed_in?
    flash[:notice] = "Your session has expired" if cookie['_utma']
    redirect_to signin_path
  end
end

这篇关于ruby on rails,会话过期通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆