简体   繁体   中英

Unable to use devise_token_auth with active_admin

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM