railsadminteam / rails_admin

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data
MIT License
7.87k stars 2.25k forks source link

Set logout_path if multiple scope (alongside devise_token_auth) #3595

Open newfylox opened 1 year ago

newfylox commented 1 year ago

Describe the bug I noticed that the logout_path is using the first scope available if there is multiple.

https://github.com/railsadminteam/rails_admin/blob/edd9f248fd3b147a95681c5f10b70e810000d3ae/app/helpers/rails_admin/application_helper.rb#L48

I'm using devise_token_auth alongside devise gem. The way that Devise::Mapping works is depending on the order that is set in routes.rb.

In my routes.rb, it's imperative that mount_devise_token_auth_for is set before devise_for. By doing this, rails_admin will have the logout_path (and other stuff) set to something like /api/v1/auth/sign_out and not /users/sign_out.

So I monkey patched it by putting this at the end of the file config/initializers/rails_admin.rb

RailsAdmin::ApplicationHelper.module_eval do
  def logout_path
    if defined?(Devise)
      scope = :user
      main_app.send("destroy_#{scope}_session_path") rescue false
    elsif main_app.respond_to?(:logout_path)
      main_app.logout_path
    end
  end
end

But now it doesn't work anymore. Even with that piece of code, the module doesn't get override anymore. I don't know if it's because I upgraded rails_admin lately or if it's because I'm not using a parent_controller anymore (using default application_controller). I don't really want to override the views because I may forget to update it when the gem is getting updates.

Is there something I can do? Is there a way to tell rails_admin the order of scope ([:user, :api_v2_user] instead of [:api_v2_user, :user]) that we want or set a global variable?

Thanks

Additional context