0

I am trying to implement both devise_token_auth and Active Admin in my rails api back-end.

There are pretty clear instructions on the devise_token_auth FAQ explaining how to implement the two together - it requires two different Application Controller classes.

# app/controllers/api_controller.rb
# API routes extend from this controller
class ApiController < ActionController::Base
  include DeviseTokenAuth::Concerns::SetUserByToken
end

# app/controllers/application_controller.rb
# leave this for ActiveAdmin, and any other non-api routes
class ApplicationController < ActionController::Base
end

I have both of these controllers in my app, but I can't figure out how to inherit from them for the controllers for Active Admin and devise_token_auth.

I'm sure I am missing something basic here, because in all of the answers that I've seen about this on StackOverflow, it seems assumed that I know how to do this.

Can anyone help?

Edit:

Restating the problem, because I don't think I was clear the first time. Right now, both Active Admin and devise_token_auth are using the ApplicationController, even though I created the ApiController too. How do I make devise_token_auth use the ApiController?

dmanaster
  • 579
  • 1
  • 6
  • 16
  • 1
    What exactly is the problem you are facing? ActiveAdmin is mounted as an engine, so you shouldn't have to extend any controller. For your API controllers, you just extend the `ApiController` class. Can you provide more details on the problem you are facing? Or are you looking for an example? – Anuj Nov 03 '20 at 06:06
  • You are right, I don't think I was clear in stating my problem. I just updated the question, but problem is that right now both Active Admin and devise_token_auth are using the ApplicationController, even though I created the ApiController too. How do I make devise_token_auth use the ApiController? An example would be perfect, so I could see how it is done. – dmanaster Nov 03 '20 at 12:11

2 Answers2

1

You can definitely use a separate namespace, that will make your code cleaner and better organized. However it is not mandatory. All you need to do is in your controllers for the APIs, extend your ApiController instead of ActionController::Base.

For example, here's a base API controller,

class Api::V1::BaseController < ActionController::API
  include DeviseTokenAuth::Concerns::SetUserByToken
  include Pundit
end

and a sample controller that extends it and uses devise_token_auth as a result.

class Api::V1::SampleController < Api::V1::BaseController
  def index
    render json: { success: true, email: current_user.email }, status: :ok
  end
end

Here's a sample project that has separate web and API controllers https://github.com/anujmiddha/rails-api-web-sample

Anuj
  • 1,912
  • 1
  • 12
  • 19
  • I'm trying to understand what you are doing here that I am not that is making this work. How does DeviseTokenAuth::RegistrationsController#create (for example) know to use the BaseController? – dmanaster Nov 03 '20 at 18:44
  • 1
    If you check out the source code for [devise_token_auth](https://github.com/lynndylanhurley/devise_token_auth/blob/master/app/controllers/devise_token_auth/registrations_controller.rb), you can see that the RegistrationsController extends [DeviseTokenAuth::ApplicationController](https://github.com/lynndylanhurley/devise_token_auth/blob/master/app/controllers/devise_token_auth/application_controller.rb) which in turn extends the DeviseController. These will still be extending the same classes, irrespective of what classes you define in your code. – Anuj Nov 04 '20 at 06:27
  • I really appreciate the effort you put in to creating the sample project - thank you! Unfortunately, I think this comment is my real answer - I can't extend RegistrationsController from my APIController. I am probably just going to remove Active Admin from my project altogether and find another way to do this. – dmanaster Nov 05 '20 at 20:57
0

you can refer to devise_token_auth demo example in devise_token_auth's readme on github.

the main difference between your case and the demo app is that you will use ApiController instead of ApplicationController. also, you have to add devise_token_auth attributes to the corresponding model, (for example, if you want to use devise_token_auth with users, you have to add the attributes to the users table and before_action: authenticate_user! to UsersController.).

icarus
  • 85
  • 2
  • 13
  • I am not having a problem getting token_auth_demo working as in the example. I can get it working fine using ApplicationController. My issue is getting it to use ApiController instead. Is it simply a matter of the namespacing? I am definitely missing something here, but I am mystified as to what it is. – dmanaster Nov 03 '20 at 13:40