Home > Back-end >  Active Admin login not working (Devise ActiveAdmin Devise JWT)
Active Admin login not working (Devise ActiveAdmin Devise JWT)

Time:04-23

I'm using rails in API mode, with Devise and Devise JWT (for the API), and ActiveAdmin. I had everything working but I've been building out the API controllers and now ActiveAdmin auth is broken and I can't figure out what's going on.

So I tried to go to /admin/login directly and it works. I enter my username and password and when I click login, I get the following error:

NoMethodError in ActiveAdmin::Devise::SessionsController#create
private method `redirect_to' called for #<ActiveAdmin::Devise::SessionsController:0x0000000001d420>

I'm not quite sure why this would be broken since it's using mostly default settings.

My routes file:

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

I haven't changed anything in ActiveAdmin::Devise and I don't even have the files showing in my codebase.

In my Devise config:

config.authentication_method = :authenticate_admin_user!
config.current_user_method = :current_admin_user

and my non-activeadmin sessions controller looks like:

# frozen_string_literal: true

module Users
  class SessionsController < Devise::SessionsController
    respond_to :json

    private

    def respond_with(resource, _opts = {})
      render json: {
        status: { code: 200, message: 'Logged in sucessfully.' },
        data: UserSerializer.new(resource).serializable_hash
      }, status: :ok
    end

    def respond_to_on_destroy
      if current_user
        render json: {
          status: 200,
          message: 'logged out successfully'
        }, status: :ok
      else
        render json: {
          status: 401,
          message: 'Couldn\'t find an active session.'
        }, status: :unauthorized
      end
    end
  end
end

And here's my admin user model:

# frozen_string_literal: true

class AdminUser < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable,
         :recoverable, :rememberable, :validatable
end

I don't believe the login is actually working when I just ignore the redirect error. I try to go to any of the pages and I get the same message You need to sign in or sign up before continuing.

Here is my application config:

    config.load_defaults 7.0
    config.api_only = true
    config.session_store :cookie_store, key: '_interslice_session'

    # Required for all session management (regardless of session_store)
    config.middleware.use ActionDispatch::Cookies
    config.middleware.use Rack::MethodOverride
    config.middleware.use ActionDispatch::Flash
    config.middleware.use ActionDispatch::Session::CookieStore

    config.middleware.use config.session_store, config.session_options

What am I doing wrong?

CodePudding user response:

Based on your error message. It seems to be root is not set for active_admin.

After signing in a user, confirming the account or updating the password, Devise will look for a scoped root path to redirect to. For instance, when using a :user resource, the user_root_path will be used if it exists; otherwise, the default root_path will be used. This means that you need to set the root inside your routes:

root to: 'home#index'

You can also override after_sign_in_path_for and after_sign_out_path_for to customize your redirect hooks.

Took a reference from here.

CodePudding user response:

I had a similar issue. In my case, it was linked to the gem responders. Here is my issue about it, still unsolved.

  • Related