Search code examples
ruby-on-rails-4http-redirectdeviseomniauth

Ruby on rails - Redirect to previous url after authentication with devise/omniauth


I'm using devise gem along with omniauth gem for Facebook, Twitter & Instagram. Below is the gem's I use:

gem 'devise'
gem 'omniauth'
gem 'omniauth-twitter'
gem 'omniauth-facebook'
gem 'omniauth-instagram'
gem 'twitter'
gem 'instagram'

I followed this toturial for Setting up Devise with Twitter and Facebook.

Inside my omniauth_callbacks_controller.rb I have set up generic_callback function like this:

def generic_callback( provider )
    @identity = Identity.find_for_oauth env["omniauth.auth"]
    @user = @identity.user || current_user
    if @user.nil?
      @tempUser = User.find_by_email(@identity.email) unless @identity.email.blank?
      if @tempUser.nil?
          @user = User.create(email: @identity.email, display_name: @identity.display_name, avatar: @identity.avatar, provider: @identity.provider || nil )
      else
        @user = @tempUser
      end
      @identity.update_attribute( :user_id, @user.id )
    end
    if @user.email.blank? && @identity.email
      @user.update_attribute( :email, @identity.email)
    end
    if @user.persisted?
      @identity.update_attribute( :user_id, @user.id )

      @user = FormUser.find @user.id
      sign_in_and_redirect @user, event: :authentication
      set_flash_message(:notice, :success, kind: provider.capitalize) if is_navigational_format?
    else
      session["devise.#{provider}_data"] = env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end 

Everything works fine and what I want to achieve is to redirect user to previous url when they sign_in or sign_up.

Before setting up omniauth gem and only used devise gem, I used this method in my application_controller.rb:

def after_sign_in_path_for(resource)
  session[:previous_url] = request.fullpath unless request.fullpath =~ /\/users/ || request.fullpath =~ /\/macbook1/
end 

This did the magic when using only devise, but now if this method is inside the application_controller.rb I get this error:

Error after sign_in or sign_out

I also tried this with error:

def after_sign_in_path_for(resource_or_scope)
    if request.env['omniauth.origin']
      request.env['omniauth.origin']
    end
  end

What do I need to do to be able to redirect user to previous url after sign_in or sign_up?


Solution

  • Inside application_controller.rb you need 2 methods: store_location & after_sign_in_path_for.

    application_controller.rb

    class ApplicationController < ActionController::Base
        after_filter :store_location
    
        def store_location
          return unless request.get?
          if (request.path != new_user_session_path &&
              request.path != new_user_registration_path &&
              request.path != "/users/password/new" &&
              request.path != "/users/password/edit" &&
              request.path != "/users/confirmation" &&
              request.path != "/users/edit" &&
              request.path != destroy_user_session_path &&
              !request.xhr?) # don't store ajax calls
            session[:previous_url] = request.fullpath
          end
        end
    
        def after_sign_in_path_for(resource)
          request.env['omniauth.origin'] || stored_location_for(resource) || root_path || request.fullpath =~ /\/admin/
        end
    
    end