mardi 31 mai 2016

How to use devise parent_controller for devise inherited controller but skip for ActiveAdmin devise controller?

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.

Aucun commentaire:

Enregistrer un commentaire