kikiki.blue kikiki.blue - 16 days ago 7
Ruby Question

How can I make specific user(not current_user) sign out on rails-devise website?

I want to know how I can make specific user(not current_user) sign out.

I saw this http://www.rubydoc.info/github/plataformatec/devise/master/Devise/Controllers/SignInOut#sign_out-instance_method and maked this code.

def kick_admin
user = User.find params[:user_id]
user.admin = false
user.save
sign_out user #want to kick him.
end


But it does not make that user sign out but make me(current_user) signed out.
What is the right way to use the sign_out method?

I checked this answer(Sign out specific user with Devise in Rails) but it was not helpful.

Answer

One way you could do this is create a new attribute in the User table, call it force_sign_out.

def kick_admin
  user = User.find params[:user_id]
  user.update_attributes(admin: false, force_sign_out: true)
end

And have a before action in ApplicatonController so that if the user attempts any activity he's signed out

class ApplicationController < ActionController::Base

  before_action :check_if_force_sign_out

  def check_if_force_sign_out
    return unless current_user.force_sign_out
    current_user.update_attributes(force_sign_out: false) # reset for non-admin log in
    sign_out
    redirect_to root_path
  end

end