ruby-on-railsrubyjwtdeviseactiveadmin

Adding Activeadmin to existing rails 6 Api with devise already installed


  1. Hey I have a rails api app with devise and jwt installed already and working perfectly fine.
  2. I installed activeadmin to handle data management but when i try to navigate to /admin i get a message on the screen("You need to sign in or sign up before continuing.") .
  3. When i try to hit /admin/login , I login as expected but i cant logout to navigate back to /admin/login.
below are my routes
# frozen_string_literal: true

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

  resources :categories do
    resources :products, shallow: true
  end
  resources :orders
  resources :line_items
  resources :carts
  root 'store#index', as: 'store_index'
  devise_for :users, path: 'api/auth', path_names: {
                                         sign_in: 'login',
                                         sign_out: 'logout',
                                         registration: 'signup'
                                       },
                     controllers: {
                       sessions: 'users/sessions',
                       registrations: 'users/registrations'
                     }

  get '/current_user', to: 'current_user#index'
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  match '*unmatched_route', via: :all, to: 'store#index', constraints: lambda { |request|
                                                                         request.path.exclude? 'rails/active_storage'
                                                                       }
end
below is application.rb
# frozen_string_literal: true

require_relative 'boot'

require 'rails'
# Pick the frameworks you want:
require 'active_model/railtie'
require 'active_job/railtie'
require 'active_record/railtie'
require 'active_storage/engine'
require 'action_controller/railtie'
require 'action_mailer/railtie'
require 'action_mailbox/engine'
require 'action_text/engine'
require 'action_view/railtie'
require 'action_cable/engine'
require 'sprockets/railtie'
# require "rails/test_unit/railtie"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module DepotApi
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 6.1

    # Configuration for the application, engines, and railties goes here.
    #
    # These settings can be overridden in specific environments using the files
    # in config/environments, which are processed later.
    #
    # config.time_zone = "Central Time (US & Canada)"
    config.eager_load_paths << Rails.root.join('lib')

    # Only loads a smaller set of middleware suitable for API only apps.
    # Middleware like session, flash, cookies can be added back manually.
    # Skip views, helpers and assets when generating a new resource.
    config.api_only = true
    config.app_generators.scaffold_controller = :scaffold_controller

    # Middleware for ActiveAdmin
    config.middleware.use Rack::MethodOverride
    config.middleware.use ActionDispatch::Flash
    config.middleware.use ActionDispatch::Cookies
    config.middleware.use ActionDispatch::Session::CookieStore
  end
end
below is applicationController
# frozen_string_literal: true

class ApplicationController < ActionController::Base
  skip_before_action :verify_authenticity_token
  before_action :configure_permitted_parameters, if: :devise_controller?

  rescue_from CanCan::AccessDenied do |_exception|
    render json: { error: 'Access denied' }, status: :forbidden
  end

  def authenticate_admin_user!
   redirect_to new_admin_user_session_path
  end

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: %i[user_name role])
    devise_parameter_sanitizer.permit(:sign_in, keys: %i[user_name role])
  end
end

here is what i get in the console

Started GET "/admin" for ::1 at 2023-07-14 02:01:10 +0300
Processing by Admin::DashboardController#index as HTML
Completed 401 Unauthorized in 11ms (ActiveRecord: 0.0ms | Allocations: 282)
  def authenticate_admin_user!
    if admin_user_signed_in?
      admin_root_path
    else
      new_admin_user_session_path
    end
  end

Solution

  • #my_app/admin/overide.rb
    ActiveAdmin::Devise::SessionsController.class_eval do
      def respond_to_on_destroy
        redirect_to new_admin_user_session_path
      end
    end