Skip to content

How To: Using ActiveAdmin to login as any user

Viktor edited this page Dec 8, 2020 · 5 revisions

As a way to provide support to end users, the ability to login to their rails app as a specific user can be useful. If you're using ActiveAdmin and Devise, adding this feature takes less than 10 minutes.

The feature consisted of two changes, customising the ActiveAdmin User show page and adding a custom member action that logs in the user.

Code Snippet 1-a: show page

show do |user|
  attributes_table do
    # Your existing columns
    # row(:id)
    # ...
    # This is where we add a new column
    row :login_as do
      form_for user, url: login_as_admin_user_path(user), method: :post do
        button "#{user.full_name}"
      end
    end
  end
end

Next we'll add the login_as member action to ActiveAdmin (http://www.activeadmin.info/docs/8-custom-actions.html)

Code Snippet 1-b: member action

# Allows admins to login as a user
member_action :login_as, method: :post do
  user = User.find(params[:id])
  # DO NOT forget this line, if you have multiple roles that can access your admin!
  authorize!(:login_as, user)
  bypass_sign_in user
  redirect_to account_path
end

....And there you have it. Now admin users can quickly log in as any user for quick trouble shooting, user testing, etc.

Taken from http://www.metachunk.com/blog/using-activeadmin-login-any-user-0 on 27th July 2013.

Clone this wiki locally