1

I'm trying to used devise_token_auth with active_admin. When running rails g active_admin:install User I get the following error.

error

usr/local/lib/ruby/gems/2.4.0/gems/actionpack-5.1.4/lib/action_dispatch/routing/route_set.rb:578:in add_route': Invalid route name, already in use: 'new_user_session' You may have defined two routes with the same name using the:as` option, or you may be overriding a route already defined by a resource with the same naming.

routes.rb

Rails.application.routes.draw do
  devise_for :users, ActiveAdmin::Devise.config
  ActiveAdmin.routes(self)
  mount_devise_token_auth_for 'User', at: 'auth'

  scope module: 'api' do
    namespace :v1 do
      resources :users, only: [:index, :show]
    end
  end

  get '/docs' => redirect('/swagger/dist/index.html?url=/apidocs/api-docs.json')
end
Antarr Byrd
  • 24,863
  • 33
  • 100
  • 188

2 Answers2

1

can you try a different approach by defining two controllers: one for api and the other for active_admin?

# 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

Now inherit all api controllers from ApiController and ActiveAdmin ones from ApplicationController.

There is a known issue between ActiveAdmin and DeviseTokenAuth

Wasif Hossain
  • 3,900
  • 1
  • 18
  • 20
1

I got it working by moving mount_devise_token_auth_for 'User', at: 'auth' into the api scope. The answer was right , here.

Rails.application.routes.draw do
  devise_for :users, ActiveAdmin::Devise.config
  ActiveAdmin.routes(self)

  constraints subdomain: 'api'do
    scope module: 'api' do
      namespace :v1 do
        resources :users, only: [:index, :show]
        mount_devise_token_auth_for 'User', at: 'auth'
      end
    end

    get '/docs' => redirect('/swagger/dist/index.html?url=/apidocs/api-docs.json')
  end
end
Antarr Byrd
  • 24,863
  • 33
  • 100
  • 188