Monday, December 18, 2017

Login page is stuck in redirection loop after using omniauth . While it was working fine before

Leave a Comment

I am working on a site that has two sides one for user and second for admin. I have used devise gem for authentication. Every thing was working fine but suddenly when I sign in to my admin account. The page is not working. I have recently worked on omniauth gem but I have not touched any previous code. I have tried to check the current_user but it is "nil". I think it is not getting user data in the application controller. Here is my code.

Application controller

class ApplicationController < ActionController::Base   add_flash_types :success, :warning, :danger, :info   protect_from_forgery prepend: true   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)     if(resource.admin==false)       '/donations/donor_history'     else       '/admins/create_account' #your path     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 

The admin controller is

class AdminsController < ApplicationController     before_action :set_admin, only: [:show, :edit, :update, :destroy, :social_media_sharing]     before_action :check_admin_level, only: [:donation_analysis]     helper_method :resource_name, :resource, :devise_mapping     before_filter :authenticate_user!     before_filter do         redirect_to new_user_session_path unless current_user && current_user.admin?     end     before_filter :index      COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]      layout "admin"     # GET /admins     # GET /admins.json     def resource_name         :admin     end      def resource         @resource ||= User.new     end      def devise_mapping         @devise_mapping ||= Devise.mappings[:admin]     end     ----------- 

user model is

class User < ApplicationRecord   # Include default devise modules. Others available are:   devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable,     :omniauthable, :omniauth_providers => [:facebook,:twitter,:linkedin]   has_many :organizations_users   has_many :organizations, through: :organizations_users      def active_for_authentication?     # Uncomment the below debug statement to view the properties of the returned self model values.     super && self.active && self.exp_alert == false   end    def self.from_omniauth(auth)     user =  where(provider: auth.provider, uid: auth.uid).first_or_create do |user|       user.email = auth.info.email       user.password = Devise.friendly_token[0,20]       user.active = 'true'       user.admin=='false'       user.exp_alert == 'false'       user.skip_confirmation!     end     user   end    def self.new_with_session(params, session)     super.tap do |user|       if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]         user.email = data["email"] if user.email.blank?       end     end   end    def self.find_or_create_from_auth_hash(auth_hash)     user = where(provider: auth_hash.provider, uid: auth_hash.uid).first_or_create do |user|       user.first_name =  auth_hash.info.nickname       user.active = 'true'       user.admin=='false'       user.exp_alert == 'false'       user.password = Devise.friendly_token[0,20]       user.token =  auth_hash.credentials.token       user.email =  "#{auth_hash.info.nickname}@test.com"       user.secret =  auth_hash.credentials.secret       user.skip_confirmation!     end     user   end    def self.linkedin_hash(auth_hash)     user = where(provider: auth_hash.provider, uid: auth_hash.uid).first_or_create do |user|       user.first_name =  auth_hash.info.first_name       user.last_name =  auth_hash.info.last_name       user.active = 'true'       user.admin=='false'       user.exp_alert == 'false'       user.password = Devise.friendly_token[0,20]       user.token =  auth_hash.credentials.token       user.email =  auth_hash.info.email       user.skip_confirmation!     end     user   end    def inactive_message     "Your Account has not been active yet."   end    def after_confirmation     super     self.update_attribute(:active, true)   end end 

routes are

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 '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 

Logs are

Started GET "/admins/create_account" for 10.0.2.2 at 2017-12-02 15:13:38 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by AdminsController#create_account as HTML   [1m[36mUser Load (1.1ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 64810987], ["LIMIT", 1]] Redirected to http://localhost:8090/users/sign_in Filter chain halted as #<Proc:0x00000002223c68@/vagrant/donation-simple/app/controllers/admins_controller.rb:6> rendered or redirected Completed 302 Found in 4ms (ActiveRecord: 1.1ms)   Started GET "/users/sign_in" for 10.0.2.2 at 2017-12-02 15:13:38 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by Devise::SessionsController#new as HTML   [1m[36mUser Load (0.8ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 64810987], ["LIMIT", 1]] Redirected to http://localhost:8090/admins/create_account Filter chain halted as :require_no_authentication rendered or redirected Completed 302 Found in 4ms (ActiveRecord: 0.8ms)   Started GET "/admins/create_account" for 10.0.2.2 at 2017-12-02 15:13:38 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by AdminsController#create_account as HTML   [1m[36mUser Load (0.8ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 64810987], ["LIMIT", 1]] Redirected to http://localhost:8090/users/sign_in Filter chain halted as #<Proc:0x00000002223c68@/vagrant/donation-simple/app/controllers/admins_controller.rb:6> rendered or redirected Completed 302 Found in 4ms (ActiveRecord: 0.8ms)   Started GET "/users/sign_in" for 10.0.2.2 at 2017-12-02 15:13:39 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by Devise::SessionsController#new as HTML   [1m[36mUser Load (1.0ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 64810987], ["LIMIT", 1]] Redirected to http://localhost:8090/admins/create_account Filter chain halted as :require_no_authentication rendered or redirected Completed 302 Found in 4ms (ActiveRecord: 1.0ms)   Started GET "/admins/create_account" for 10.0.2.2 at 2017-12-02 15:13:39 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by AdminsController#create_account as HTML   [1m[36mUser Load (1.1ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 64810987], ["LIMIT", 1]] Redirected to http://localhost:8090/users/sign_in Filter chain halted as #<Proc:0x00000002223c68@/vagrant/donation-simple/app/controllers/admins_controller.rb:6> rendered or redirected Completed 302 Found in 5ms (ActiveRecord: 1.1ms)   Started GET "/users/sign_in" for 10.0.2.2 at 2017-12-02 15:13:39 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by Devise::SessionsController#new as HTML   [1m[36mUser Load (0.8ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 64810987], ["LIMIT", 1]] Redirected to http://localhost:8090/admins/create_account Filter chain halted as :require_no_authentication rendered or redirected Completed 302 Found in 4ms (ActiveRecord: 0.8ms)   Started GET "/admins/create_account" for 10.0.2.2 at 2017-12-02 15:13:39 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by AdminsController#create_account as HTML   [1m[36mUser Load (0.7ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 64810987], ["LIMIT", 1]] Redirected to http://localhost:8090/users/sign_in Filter chain halted as #<Proc:0x00000002223c68@/vagrant/donation-simple/app/controllers/admins_controller.rb:6> rendered or redirected Completed 302 Found in 3ms (ActiveRecord: 0.7ms)   Started GET "/users/sign_in" for 10.0.2.2 at 2017-12-02 15:13:39 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by Devise::SessionsController#new as HTML   [1m[36mUser Load (0.7ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 64810987], ["LIMIT", 1]] Redirected to http://localhost:8090/admins/create_account Filter chain halted as :require_no_authentication rendered or redirected Completed 302 Found in 3ms (ActiveRecord: 0.7ms)   Started GET "/admins/create_account" for 10.0.2.2 at 2017-12-02 15:13:39 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by AdminsController#create_account as HTML   [1m[36mUser Load (0.9ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 64810987], ["LIMIT", 1]] Redirected to http://localhost:8090/users/sign_in Filter chain halted as #<Proc:0x00000002223c68@/vagrant/donation-simple/app/controllers/admins_controller.rb:6> rendered or redirected Completed 302 Found in 4ms (ActiveRecord: 0.9ms)   Started GET "/users/sign_in" for 10.0.2.2 at 2017-12-02 15:13:39 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by Devise::SessionsController#new as HTML   [1m[36mUser Load (1.0ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 64810987], ["LIMIT", 1]] Redirected to http://localhost:8090/admins/create_account Filter chain halted as :require_no_authentication rendered or redirected Completed 302 Found in 4ms (ActiveRecord: 1.0ms)   Started GET "/admins/create_account" for 10.0.2.2 at 2017-12-02 15:13:39 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by AdminsController#create_account as HTML   [1m[36mUser Load (0.7ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 64810987], ["LIMIT", 1]] Redirected to http://localhost:8090/users/sign_in Filter chain halted as #<Proc:0x00000002223c68@/vagrant/donation-simple/app/controllers/admins_controller.rb:6> rendered or redirected Completed 302 Found in 4ms (ActiveRecord: 0.7ms)   Started GET "/users/sign_in" for 10.0.2.2 at 2017-12-02 15:13:39 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by Devise::SessionsController#new as HTML   [1m[36mUser Load (1.0ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 64810987], ["LIMIT", 1]] Redirected to http://localhost:8090/admins/create_account Filter chain halted as :require_no_authentication rendered or redirected Completed 302 Found in 4ms (ActiveRecord: 1.0ms)   Started GET "/admins/create_account" for 10.0.2.2 at 2017-12-02 15:13:39 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by AdminsController#create_account as HTML   [1m[36mUser Load (0.8ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 64810987], ["LIMIT", 1]] Redirected to http://localhost:8090/users/sign_in Filter chain halted as #<Proc:0x00000002223c68@/vagrant/donation-simple/app/controllers/admins_controller.rb:6> rendered or redirected Completed 302 Found in 4ms (ActiveRecord: 0.8ms)   Started GET "/users/sign_in" for 10.0.2.2 at 2017-12-02 15:13:39 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by Devise::SessionsController#new as HTML   [1m[36mUser Load (0.7ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 64810987], ["LIMIT", 1]] Redirected to http://localhost:8090/admins/create_account Filter chain halted as :require_no_authentication rendered or redirected Completed 302 Found in 4ms (ActiveRecord: 0.7ms)   Started GET "/admins/create_account" for 10.0.2.2 at 2017-12-02 15:13:40 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by AdminsController#create_account as HTML   [1m[36mUser Load (0.7ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 64810987], ["LIMIT", 1]] Redirected to http://localhost:8090/users/sign_in Filter chain halted as #<Proc:0x00000002223c68@/vagrant/donation-simple/app/controllers/admins_controller.rb:6> rendered or redirected Completed 302 Found in 3ms (ActiveRecord: 0.7ms)   Started GET "/users/sign_in" for 10.0.2.2 at 2017-12-02 15:13:40 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by Devise::SessionsController#new as HTML   [1m[36mUser Load (0.7ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 64810987], ["LIMIT", 1]] Redirected to http://localhost:8090/admins/create_account Filter chain halted as :require_no_authentication rendered or redirected Completed 302 Found in 3ms (ActiveRecord: 0.7ms)   Started GET "/admins/create_account" for 10.0.2.2 at 2017-12-02 15:13:40 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by AdminsController#create_account as HTML   [1m[36mUser Load (0.8ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 64810987], ["LIMIT", 1]] Redirected to http://localhost:8090/users/sign_in Filter chain halted as #<Proc:0x00000002223c68@/vagrant/donation-simple/app/controllers/admins_controller.rb:6> rendered or redirected Completed 302 Found in 4ms (ActiveRecord: 0.8ms)   Started GET "/users/sign_in" for 10.0.2.2 at 2017-12-02 15:13:40 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by Devise::SessionsController#new as HTML   [1m[36mUser Load (1.4ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 64810987], ["LIMIT", 1]] Redirected to http://localhost:8090/admins/create_account Filter chain halted as :require_no_authentication rendered or redirected Completed 302 Found in 5ms (ActiveRecord: 1.4ms)   Started GET "/admins/create_account" for 10.0.2.2 at 2017-12-02 15:13:40 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by AdminsController#create_account as HTML   [1m[36mUser Load (1.6ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 64810987], ["LIMIT", 1]] Redirected to http://localhost:8090/users/sign_in Filter chain halted as #<Proc:0x00000002223c68@/vagrant/donation-simple/app/controllers/admins_controller.rb:6> rendered or redirected Completed 302 Found in 6ms (ActiveRecord: 1.6ms)   Started GET "/users/sign_in" for 10.0.2.2 at 2017-12-02 15:13:40 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by Devise::SessionsController#new as HTML   [1m[36mUser Load (1.1ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 64810987], ["LIMIT", 1]] Redirected to http://localhost:8090/admins/create_account Filter chain halted as :require_no_authentication rendered or redirected Completed 302 Found in 5ms (ActiveRecord: 1.1ms)   Started GET "/admins/create_account" for 10.0.2.2 at 2017-12-02 15:13:40 +0000 Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by AdminsController#create_account as HTML   [1m[36mUser Load (0.8ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m  [["id", 64810987], ["LIMIT", 1]] Redirected to http://localhost:8090/users/sign_in Filter chain halted as #<Proc:0x00000002223c68@/vagrant/donation-simple/app/controllers/admins_controller.rb:6> rendered or redirected Completed 302 Found in 4ms (ActiveRecord: 0.8ms) 

enter image description here

1 Answers

Answers 1

I have solved it my self and now I am sharing this Answer so that it may help anyone else in the future.

The problem was the current_user variable which was overriding in the application controller.

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

Devise set the current_user variable itself and this code was overriding the current_user variable. The strange thing is that the same code was working before without any problem. I think devise has updated. However after comment the above code every thing is working fine.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment