Undefined method 'authenticate_with_http_basic' in Rails 4.2

I am trying to use Devise for server side API authentication. In my controller, I use the code directly from the Devise wiki to use HTTP Basic auth ...

   before_filter :check_auth, only: [:show] 

and

  def check_auth
    authenticate_or_request_with_http_basic do |username,password|
      resource = User.find_by_email(username)
      if resource.valid_password?(password)
        sign_in :user, resource
      end
    end
  end

When I navigate to the intended URL, I expect 401 Unauthorized JSON error, but instead I get the following error:

undefined method `authenticate_or_request_with_http_basic' for #<V1::ItemsController:0x007fb6b3d79120>

I do not understand why this method will not be defined. I use RubyMine and IDE links directly to the code for this method, but for some reason something is wrong. Can someone help me figure out what I'm doing wrong?

UPDATE . I was able to identify my problem. I use the Rails API against full Rails, so the module for HttpAuthentication (surprisingly) is not included.

By adding:

include ActionController::HttpAuthentication::Basic::ControllerMethods

ApplicationController, .

+4
1

, Rails API Rails authenticate_with_http_basic. application_controller.rb.

//application_controller.rb

include ActionController::HttpAuthentication::Basic::ControllerMethods
include ActionController::HttpAuthentication::Token::ControllerMethods
+6

All Articles