Wednesday, 5 December 2018

switch user -- users list click any user for session create

Please visit the above doc :

https://github.com/flyerhzm/switch_user
======================================

Add Gem:

gem "switch_user"
=======================================
If you need to override the default configuration, run rails g switch_user:install and a copy of the configuration file will be copied to config/initializers/switch_user.rb in your project.

rails g switch_user:install

===========================================

Add file for controller:

app/controllers/switch_user_controller.rb

class SwitchUserController < ApplicationController
  def set_current_user
  user = User.find(params[:scope_identifier])
                # without devise
  if user.present?
  session[:user_id] = user.id
  redirect_to '/'
  end
                # with devise 
  # sign_in(user)
  end

  def remember_user
  end

end

==============================================

config/routes.rb

get 'switch_user', to: 'switch_user#set_current_user'

  get 'switch_user/remember_user', to: 'switch_user#remember_user'


=============================================

Add Link for Button:

1. Activeadmin Users index page
   
     column "Switch User" do |show|
        links = link_to 'Login As', "/switch_user?scope_identifier=#{show.id}"
     end

2. simple rails users index
   
    <%= link_to 'Login As', "/switch_user?scope_identifier=#{user.id}"%>
==============================================








No comments:

Post a Comment