Reputation: 1511
I am developing Api based application, site.com (Client App), api.site.com (Server App)
In my api.site.com, there are passwords, confirmations controller, which are inherited from the Devise controllers. By default Devise parent controller is Application controller, but Devise inherited controllers need to pass through ApiBaseController api_authentication action. So, Devise.rb has following configuration:
config.parent_controller = 'ApiBaseController'
Api authentication is working fine now.
ApiBaseController sample code:
class ApiBaseController < ApplicationController
before_action :api_authentication
def api_authentication
api_key = request.headers['Api-Key']
@app = Application.find_by_api_key(api_key) if api_key
unless @app
return render json: { errors: { message: 'Something went wrong, contact admin', code: '1000' } }
end
end
end
Now i am using ActiveAdmin, after installing ActiveAdmin i tried to open http://localhost:3000/admin/login on browser, I saw following error response on browser instead of active admin login page:
{"errors":{"message":"Something went wrong, contact admin","code":1000}}
I checked the issue, and i realized that active_admin/devise/sessions
controller also passed through ApiBaseController. This is because we had set our parent controller to ApiBaseController (config.parent_controller = 'ApiBaseController'
). I removed the code and ActiveAdmin worked fine.
But passwords, confirmations controller did not passed through the ApiBaseController api_authentication() since i removed the Devise configuration (config.parent_controller = 'ApiBaseController'
).
So if you guys have understood the problem, please let me know the solution.
In summary, i need all the api Devise inherited controllers need to pass through ApiBaseController for api_authentication() check and ActiveAdmin Devise controllers do not need to pass through ApiBaseController.
Thanks in advance.
Upvotes: 1
Views: 3102
Reputation: 1511
I was searching the way to add the condition inside the Devise parent_controller, but i did not get any solution for Devise. But, i solved it by adding some code.
class ApiBaseController < ApplicationController
before_action :api_authentication
def api_authentication
return true if params[:controller].include?("active_admin/devise/")
api_key = request.headers['Api-Key']
@app = Application.find_by_api_key(api_key) if api_key
unless @app
return render json: { errors: { message: 'Something went wrong, contact admin', code: '1000' } }
end
end
end
Upvotes: 1
Reputation: 2102
You just write you api authentication logic in application_controller.rb
and use before_filter
in your password controller or where ever you want.
class ApplicationController < ActionController::Base
private
def api_authentication
api_key = request.headers['Api-Key']
@app = Application.find_by_api_key(api_key) if api_key
unless @app
return render json: { errors: { message: 'Something went wrong, contact admin', code: '1000' } }
end
end
end
and use before_filter :api_authentication
in your controller
class PasswordsController < Devise::PasswordsController
before_filter :api_authentication
.......
end
Upvotes: 2