简体   繁体   中英

ActiveAdmin with devise

I have a project using devise_token_auth for authentication. I have installed Active Admin following this .

When I try to access localhost:3000/admin I get You need to sign in or sign up before continuing.

However, when I comment config.authentication_method = :authenticate_admin_user! in config/initializers/active_admin.rb , localhost:3000/admin opens the dashboard page.

My question is why am I not getting the login page for active admin?

There are several things that you need to know when working with both ActiveAdmin (AA) and devise_token_auth . AA uses:

  • Devise for authentication
  • :admin as default namespace

It means that all of your AA resources will have routes under /admin eg /admin/posts and they will be authenticated using Devise ; not devise_token_auth .

In order to avail both types of authentication system, you must use two namespaces: one for AA and one for devise_token_auth.

A common strategy in this scenario would be to define AA routes before devise_token_auth like so:

Rails.application.routes.draw do

  # AA routes available at /admin
  devise_for :admin_users, ActiveAdmin::Devise.config
  ActiveAdmin.routes(self)

  # token auth routes available at /api/v1/auth
  namespace :api do
    scope :v1 do
      mount_devise_token_auth_for 'User', at: 'auth'
    end
  end

end

Here AA is using :admin_users and token_auth will use :users table. Don't forget to adapt them to your needs.

Note : If you ever face trouble with your ApplicationController while working with AA and devise_token_auth, please refer to this link .

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