【问题标题】:authenticate_with_http_token evaluates to nilauthenticate_with_http_token 评估为 nil
【发布时间】:2015-02-08 02:50:07
【问题描述】:

我正在尝试创建一个非常基本的身份验证器,但不幸的是我得到了 nil 作为回报,导致我的路由到 401,因为它未经授权。

[20] pry(#<API::UsersController>)> 
ActionController::HttpAuthentication::Token::ControllerMethods.authenticate_with_http_token
NoMethodError: undefined method `authenticate_with_http_token' for     
ActionController::HttpAuthentication::Token::ControllerMethods:Module

[22] pry(#<API::UsersController>)> cd    
ActionController::HttpAuthentication::Token::ControllerMethods

[23] pry(#<Module>):1> ls
ActionController::HttpAuthentication::Token::ControllerMethods#methods:
authenticate_or_request_with_http_token  authenticate_with_http_token      
request_http_token_authentication
locals: _  __  _dir_  _ex_  _file_  _in_  _out_  _pry_

[24] pry(#<Module>):1>

代码:

protected
  def authenticate
    authenticate_token || render_unauthorized
  end

  def authenticate_token
    binding.pry
    authenticate_with_http_token do |token, options|
      User.find_by(auth_token: token)
    end
  end

  def render_unauthorized
    self.headers['WWW-Authenticate'] = 'Token realm="Application"'
    render json: 'Bad credentials', status: 401
  end  

【问题讨论】:

    标签: ruby-on-rails authentication http-token-authentication


    【解决方案1】:

    似乎您的API::UsersController(或者您的ApplicationController)需要:

    include ActionController::HttpAuthentication::Token::ControllerMethods
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-07
      • 2021-12-26
      • 2016-08-25
      • 1970-01-01
      • 1970-01-01
      • 2014-06-13
      • 2014-09-12
      • 1970-01-01
      相关资源
      最近更新 更多