Redirect User to a Specific Page After Sign In With Devise

Devise is a great gem that allows you to authenticate users and manage sessions in your application. At times you may want to overwrite the devise code a bit to perform a task your application needs. I had to do the same the other day and the solution might help you too if you happen to be wanting to have the user visit the current page after logging in.

Context:

User is on purchase page but is not logged in. If purchase attempt is made with an email already in the database, user is prompted to log in and soon after he/she needs to be redirected to that specific purchase page where he/she can complete the transaction.

After some research I used some of the code from the How to in the plataformatec/devise github page.

In the ApplicationController I wrote


after_filter :store_location

def store_location
  # store last url - this is needed for post-login redirect to whatever the user last visited.
    if (request.fullpath != "/users/sign_in" &&
        request.fullpath != "/users/sign_up" &&
        request.fullpath != "/users/password" &&
        request.fullpath != "/users/sign_out" &&
        !request.xhr?) # don't store ajax calls

      session[:previous_url] = new_purchase_path(params["product”]) if params["product"]
   
   end
 end

  def after_sign_in_path_for(resource)
    session[:previous_url] || root_path
  end

The code above is storing the session[:previous_url] only if params[“product”] exists.

Although this worked it was not the most elegant solution I could find. As far as I could tell devise stores previous url as session[:user_return_to] for a visitor tries to access a page that it does not have access unless user logs in.

Having this in mind I went to my registrations controller and I set session[:user_return_to] to the path of a purchase page.


class RegistrationsController < Devise::RegistrationsController

def new
    @user = User.new
    @purchase = Purchase.new
    session[:user_return_to] = request.fullpath
    super
  end

def create
    @user = User.new(user_params)
    unless @user.save
     @purchase = Purchase.new
      return render 'new'
    end
    sign_in @user
    session[:just_signed_up] = true
    session.delete(:user_return_to)
 end


In this Registration Controller session[:user_return_to] is set to request.fullpath in the method new. Request.fullpath by the way returns the string full path of the last url requested.
Just so the session[:user_return_to] is not saved to more areas than it should, I make sure it gets deleted in the create method. The session.delete(:user_return_to) is erasing the value of key “user_return_to”.

Advertisements

One thought on “Redirect User to a Specific Page After Sign In With Devise

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s