Search code examples
ruby-on-railsdeviseactiveadmincancancandevise-token-auth

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?


Solution

  • 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 e.g. /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.