简体   繁体   English

处理Rails身份验证红宝石中不存在的cookie

[英]handling non-existent cookie in authentication ruby on rails

I am trying to set up user authentication in my ruby on rails app. 我正在尝试在我的ruby on rails应用程序中设置用户身份验证。 I have the following code in app/controllers/application_controller.rb. 我在app / controllers / application_controller.rb中有以下代码。 Code fails when I try to reference the remember_token cookie before it has been set. 当我尝试在设置之前记住Remember_token cookie时,代码将失败。 How do I fix this? 我该如何解决? Attempted to do it with cookies.has_key(:remember_token) (see below) which failed because cookies is not a hash (I think). 尝试使用cookies.has_key(:remember_token)(见下文)执行此操作,但由于cookie不是哈希(我认为)而失败。

  protect_from_forgery
include SessionsHelper
#before_filter :authenticate 

def authenticate
    @test={:email=>"dummy", :password=>"dummy"}
    @test=cookies.permanent.signed[:remember_token] if cookies.permanent.signed.has_key(:remember_token)==true

    if User.authenticate(@test[:email], @test[:password])[:allow]==false
        redirect_to :controller=>'sessions', :action=>'new'
    end

end

您可以执行以下操作:

@test=cookies.permanent.signed[:remember_token] unless cookies.permanent.signed[:remember_token].nil?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM