Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to override current_user helper method of devise gem

How can i override current_user of devise gem. Actually I need to add web services for mobile-app.

Currently devise is managing session and 'current_user' for web-application.

Now Mobile app will send user_id to the server. I need to override current user like this

def current_user   if params[:user_id].blank?     current_user   else     User.find(params[:user_id])   end    end 

Should I need to modify devise gem as plugin ? or something else ?
Kindly explain in detail as I am new in rails.

Kind regards,

like image 992
Omer Aslam Avatar asked Apr 27 '12 11:04

Omer Aslam


People also ask

How does devise Current_user work?

current_user works by storing id of current user in the application session. Most commonly session is stored in cookies. Whether or not the cookies survive browser restart depends on client's browser settings.

What is devise in Ruby on Rails?

Devise is the cornerstone gem for Ruby on Rails authentication. With Devise, creating a User that can log in and out of your application is so simple because Devise takes care of all the controllers necessary for user creation ( users_controller ) and for user sessions ( users_sessions_controller ).


1 Answers

According to the module Devise::Controllers::Helpers, current_user (together with all other devise helpers) is added to ApplicationController, which means that you can override it in this way:

# in application_controller.rb  def devise_current_user   @devise_current_user ||= warden.authenticate(scope: :user) end  def current_user   if params[:user_id].blank?     devise_current_user   else     User.find(params[:user_id])   end    end 
like image 127
Limbo Peng Avatar answered Sep 18 '22 13:09

Limbo Peng