pizza247
pizza247

Reputation: 3897

basic auth with rails-api

I'm sure that I'm doing something wrong here, but this is what my application controller looks like:

class ApplicationController < ActionController::API                                                                                                                                                                                                        
  include ActionController::HttpAuthentication::Basic                                                                                                                                                                                                      
  include ActionController::MimeResponds                                                                                                                                                                                                                   

  http_basic_authenticate_with :name => "joeyjojo", :password => "shabadoo"
end

I can't figure out why my http_basic_authenticate_with is throwing this error:

undefined method `http_basic_authenticate_with' for ApplicationController:Class

I'm sure it's something simple, but I don't see it. The MimeResponds is working just fine in other controllers.

Upvotes: 23

Views: 6683

Answers (2)

Carlos Antonio
Carlos Antonio

Reputation: 541

You have to include ActionController::HttpAuthentication::Basic::ControllerMethods instead, to have the methods available. Here's the module in ActionController::Base

Upvotes: 29

Fergara
Fergara

Reputation: 947

If you have a Rails API, add this to your controller:

include ActionController::HttpAuthentication::Basic::ControllerMethods

class YourController < ApplicationController

  include ActionController::HttpAuthentication::Basic::ControllerMethods
  http_basic_authenticate_with name: "username", password: "passwd123"

I am using Rails 4.2.4 and my ApplicationController:

class ApplicationController < ActionController::API

Upvotes: 14

Related Questions