无法将 devise_token_auth 与 active_admin 一起使用

Unable to use devise_token_auth with active_admin

我正在尝试将 devise_token_auth 与 active_admin 结合使用。当 运行 rails g active_admin:install User 我得到以下错误。

错误

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

您可以通过定义两个控制器来尝试不同的方法:一个用于 api,另一个用于 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

现在继承 ApiController 的所有 api 控制器和 ApplicationControllerActiveAdmin 控制器。

There is a known issue between ActiveAdmin and DeviseTokenAuth

我通过将 mount_devise_token_auth_for 'User', at: 'auth' 移动到 api 范围来让它工作。答案是正确的,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