Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

authenticate_with_http_token evaluates to nil

I'm trying to create a very basic authenticator but unfortunately I'm getting nil in return causing my route to 401 since its unauthorized.

[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>

Code:

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  
like image 400
alvincrespo Avatar asked Dec 01 '25 14:12

alvincrespo


1 Answers

Seems like your API::UsersController (or alternatively your ApplicationController) needs:

include ActionController::HttpAuthentication::Token::ControllerMethods
like image 56
AWM Avatar answered Dec 04 '25 06:12

AWM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!