Rails会话管理 [英] Rails Session Management

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

问题描述



我有以下内容:

  def login 
if request.post?
if params [:remember_me]
#这是用户检查记住我的框
cookies [:login] = {:value => XJ12,:expires => Time.now + 120}
end

session_user = User.authenticate(params [:user] [:email],params [:user] [:password])
if session_user

session [:user] = session_user.id
@ user = User.find(session [:user])
@ user.update_attributes(:last_login_time =>时间.now(),:is_logged => true)
@ user.save
flash [:message] =登录成功。
redirect_to/ admin
else
flash [:warning] =您的电子邮件或密码不正确,请重新输入。
end
end
end

end



无论我做什么,cookie都会自动设置(EXPIRES:session)。我想做,如果用户没有选择记住我的选项,我设置cookie为10分钟。如果他选择记住我,那么我将把它设置更长的时间。
我可以得到一些指导如何做吗?

解决方案

首先,Time.now + 120只有2分钟



然后,这里可以是你的流程:


  1. 尝试获取用户时,请检查是否设置了Cookie(我建议您为每个用户创建一个唯一的随机字符串,因为Cookie可以在客户端修改)

  2. 如果设置了Cookie,请记录相应的用户

  3. 如果没有,请尝试会话中的基本日志

  4. 如果用户已选中框


I am trying to implement remember me option for login based on the code that I inherited.

I have following:

def login 
if request.post?
  if params[:remember_me]
    # this is where user checked the remember me box
    cookies[:login] = { :value => "XJ12", :expires => Time.now + 120}
  end

    session_user = User.authenticate(params[:user][:email], params[:user][:password])   
    if session_user

      session[:user] = session_user.id
      @user=User.find(session[:user])
      @user.update_attributes(:last_login_time => Time.now(),:is_logged => true)
      @user.save
      flash[:message]  = "Login successful."
      redirect_to "/admin"
    else
      flash[:warning] = "Your email or password is incorrect. Please re-enter."
    end
  end
end

end

No matter what I do, the cookie is automatically set (EXPIRES: session). I would like to make if user did not select remember me option, that I set cookie for 10 mins. If he selected remember me, then I will set it for much longer time. Can I get some guidance as what to do?

解决方案

Firstly, Time.now + 120 is only 2 minutes from now, you should set it to a later value.

Then, here can be your flow :

  1. When trying to get the user, check if a cookie is set (I recommend you creating a unique random string for each user, since the cookies can be modified client side)
  2. If a cookie is set, log the corresponding user
  3. If not, try the basic log from session
  4. When connecting, register the cookie if the user has checked the box

这篇关于Rails会话管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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