devise配置登录超时和单处登陆

左手 posted @ 2014年5月06日 20:19 in 积累点滴 with tags rails devise 登录超时 devise单用户登录 , 3409 阅读

1.配置用户超时
<Project>/config/initializers/devise.rb
编辑 # config.timeout_in = 30.minutes 这一行,修改为想要配置的时间

<Project>/app/models/user.rb
配置devise :database_authenticatable, :registerable,这一行,加上 :timeoutable

2.设置用户单处登陆
原理:devise在用户登陆后会设置users表中的current_sign_in_at字段为最后一次登陆的时间,用户每次登陆后在session中保存current_sign_in_at字段,在application中校验session中current_sign_in_at字段,如果为空或者小于current_user对象的current_sign_in_at则强制注销
1) 在application_controller基类中定义after_sign_in_path_for方法在当中设置session对象

  def after_sign_in_path_for(resource)
    session[:current_sign_in_at] = current_user.current_sign_in_at
    root_path
  end

2) 定义has_signed取代:authenticate_user!判断用户登陆

  def has_signed
    unless user_signed_in?
      redirect_to root_path
    else
      if session[:current_sign_in_at] == nil or session[:current_sign_in_at] < current_user.current_sign_in_at
        redirect_to destroy_user_session_path
      end
    end
  end

3)在需要判断用户登陆的controller中添加before_action :has_signed过滤器。

Avatar_small
BSNL Tune 说:
2022年8月08日 15:52

BSNL Personalized Ring Back Tone services, a mobile subscriber greet their callers with their favorite song as best choice, and this new My BSNL Tunes App service is an innovative service help the subscribers to express themselves, access information, entertainment and connect the other by greeting. BSNL Tune Most popular albums are vying for honor, but to find the best with codes or by names it is difficult, but now with this new launch of My BSNL Tunes App, each mobile user now have a chance to listen and activate their favorite songs of any language to set as BSNL caller tune on their mobiles.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter