Search code examples
ruby-on-railsrubydeviseomniauth

Devise is not assigning data to current_user,resource variables after successfull signin


I am stuck in a problem that after integrating omniauth in my application devise is behaving strange and it is not assigning logged in user to variables current_user, resource.

Where I think the problem is

I have checked all the things and I think the problem is with this line in the application controller.

def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end

I have checked the session and it has no data in `.

The same code was working fine but after omniauth every thing is got ruined. I am worried that how to solve it now.

Application Controller

Here is the code of my application controller

class ApplicationController < ActionController::Base
  add_flash_types :success, :warning, :danger, :info
  protect_from_forgery prepend: true
  #before_filter :require_no_authentication, only: [:new, :create, :cancel]
  before_filter :configure_permitted_parameters, if: :devise_controller?
  layout :layout_by_resource

 def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end

  helper_method :current_user

  def logged_in_using_omniauth
    session[:logged_in_using_omniauth].present?
  end
  helper_method :logged_in_using_omniauth

  private

  def layout_by_resource
    if devise_controller?
      "admin"
    else
      "application"
    end
  end

  protected

  def after_sign_in_path_for(resource)
    #console

    if(resource.admin)
      #render plain: resource
      #console
      '/admins'
    else
      '/donations/donor_history'
    end
  end

  def after_sign_out_path_for(resource)
    '/users/sign_in' #your path
  end

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up) do |user_params|
      user_params.permit(:admin, :email, :password, :password_confirmation,:first_name,:last_name)
    end
    devise_parameter_sanitizer.permit(:account_update) do |user_params|
      user_params.permit(:admin, :email, :password, :password_confirmation,:current_password,:first_name,:last_name)
    end
  end

end

error message, "NoMethodError undefined method id for nil class"

Routes

Here is the code of routes

Rails.application.routes.draw do

  devise_for :users,  controllers: {confirmations: 'confirmations',registrations: 'users/registrations',omniauth_callbacks: 'users/omniauth_callbacks' } do
    get "confirmation", to: "confirmations#after_confirmation_path_for"
     delete 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
  end

  devise_for :models

  get 'donor_email_receipt', :to => 'donations#donor_email_receipt', :as => :donor_email_receipt
  get 'donations/donor_history/' => 'donations#donor_history'
  get 'donations/donor_signup/' => 'donations#donor_signup'
  post 'donations/donor_signup/' => 'donations#donor_signup'
  post 'donations/sms_service/' => 'donations#sms_service'
  post 'donations/create_user_account' => 'donations#create_user_account'
  post 'donations/add_user_payroll' => 'donations#add_user_payroll'

  resources :donations, except: [:new, :create]

  resources :campaigns do
    resources :donations, only: [:new, :create, :create_user_account]
    get 'donations/create_user_account' => 'donations#create_user_account'
  end
  resources :organizations

  post 'admins/social_sharing_switch/' => 'admins#social_sharing_switch'
  get 'admins/error_detail/' => 'admins#error_detail'
  get 'admins/generate_report/:id' => 'admins#generate_report'
  get 'admins/create_company/' => 'admins#create_company'
  post 'admins/create_company/' => 'admins#create_company'
  get 'admins/revenue_detail/' => 'admins#revenue_detail'
  get 'admins/create_account' => 'admins#create_account'
  get 'admins/view_account' => 'admins#view_account'
  get 'admins/view_company/:id' => 'admins#view_company'
  constraints RouteConstraint.new do
    get 'admins/donation_analysis' => 'admins#donation_analysis'
  end

  get 'admins/link_expiry' => 'admins#link_expiry'
  get 'admins/edit_profile' => 'admins#edit_profile'
  post 'admins/update_profile' => 'admins#update_profile'
  match '/admins/create_account', to: 'admins#create_account', via: 'post'
  match '/admins/:id', to: 'admins#destroy', via: 'get' , as: 'admin_destroy'

  resources :admins

  get 'crons/expirylink_alert' => 'crons#expirylink_alert'


  devise_scope :user do
    get '/users/sign_out' => 'devise/sessions#destroy'
  end

  def user_params
  params.require(:user).permit(:name, :email, :password, :password_confirmation)
end


  root to: "campaigns#latest"
end

Solution

  • You NOT have to define current_user method ( def current_user ) in application controller because Devise already do.

    You only have to call current_user variable to get the current user, like

    def index
        if current_user.first_name == "Jhon"
            redirect_to :other_path
        end
    end