Skip to content

After masquerading, return to page I started masquerading from #148

Open

Description

@matt17r

I click the masquerade button on the admin_user_path(:id) (e.g. https://example.com/admin/users/10122496). When I stop masquerading, I'd like to return to that same page.

  • I thought Devise.masquerade_routes_back = true would do it but that seems to try to take you to that page when you start masquerading, not when stopping
  • I tried overriding after_back_masquerade_path_for(resource) (code below) but it seems to be too late to grab the masqueraded user. Both resource and current_user refer to (what was) the masquerading_current_user
  • I hoped maybe I could use *opts with back_masquerade_path(current_user) but I'm not sure where to start
class Admin::MasqueradesController < Devise::MasqueradesController
  protected

  def after_back_masquerade_path_for(user)
    # `user` and `current_user` are both the masquerading user, not the masqueraded user
    admin_user_path(user.id)
  end
end

Am I missing something obvious?

Activity

  1. o2h-vipul commented on May 29, 2025

    @o2h-vipul

    Same is happening with me. Thanks for pointing out @matt17r

  2. o2h-vipul commented on May 29, 2025

    @o2h-vipul

    I have found the solution you have to make the Devise.masquerade_routes_back = false in devise initialize and now if you add the

    class Admin::MasqueradesController < Devise::MasqueradesController
      protected
    
      def after_back_masquerade_path_for(user)
        # `user` and `current_user` are both the masquerading user, not the masqueraded user
        admin_user_path(user.id)
      end
    end
    

    that would work.
    I hope this will help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions