Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Redirect Back To Same URL after Login through Devise in Rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.41k
    Comment on it

    In almost every web application there are certain things which a user is allowed to do only after he or she is logged  in into the website. If user  trying to access some page or some URL where he/she can get access only after logging into  the website then he should be redirected back to the same page as soon as he logs in after filling in his ID and Password. 

    To achieve this functionality we can use the requested path and save it to be used again once the user is logged in.

    If we are using the gem Devise our authentication purposes then a way to achieve this functionality is as follows:

    For example, if we are creating an e-commerce application where a user can add some products to his cart for shopping purposes, then he or she is redirected to login page to add the item to his cart. 

    For this, as soon as the user will press add to cart button, the control will go the cart controller where we will call

    these two methods on the top :

    class CartsController < ApplicationController
      before_action :store_location,:authenticate_user!
      
      def create
    
      end
    
      ....
    
    end

    This shows us that as soon as a request will come to the carts controller the before action will call these two methods. The authenticate user method will take the user to the login form to log into the web site and store location method will be defined in the application controller in which we will save our previous URL

    class ApplicationController < ActionController::Base 
    
     def store_location
        if(request.path != "/users/sign_in" &&
          request.path != "/users/sign_up" &&
          request.path != "/users/password/new" &&
          request.path != "/users/password/edit" &&
          request.path != "/users/confirmation" &&
          request.path != "/users/sign_out" &&
          !request.xhr? && !current_user) # don't store ajax calls
          session[:previous_url] = request.fullpath
        end
      end
    
      def after_sign_in_path_for(resource)
        previous_path = session[:previous_url]
        session[:previous_url] = nil
        previous_path || root_path
      end
    
      ...
    
    end

    The store location method will save the full URL in a session hash. This hash is used in the after sign in path method of a device which is used to redirect a user to a particular URL after sign in. In this method if the session hash receives a value of a previous URL then it is redirected to that page or it will be redirected to the root path.

    So this is how we can redirect a user back to the same page from where he was redirected to login page.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: