Skip to content Skip to sidebar Skip to footer

Navigation-based Redirections After Sign Up

My website allows users to browse freely up to the point where they need to be signed in to continue using the service (hitting the 'sign up' button and going through the registrat

Solution 1:

Both via hidden form fields and sessions have benefits. Using sessions in the controller would be the simplest for you to keep track of as far as development goes however if the user has multiple tabs open it could lead to an odd page flow if they jump between tabs. This is where hidden form fields work out a bit better but are more annoying to maintain. I tend to go with the sessions method and do something like the following in a controller action:

#This will store the page they're coming from to get to this action.
session[:interesting_page] = request.env["HTTP_REFERER"] || root_path

#Or store the current interesting page within the interesting page's controller action
 session[:interesting_page] = request.original_url

Solution 2:

One way is simply to use HTTP_REFERER to redirect on sign in. HTTP_REFERER is of course limited since some clients do not send it. But you could also argue that if the client does not send the header they don't want to be tracked.

Another solution is to use the session:

class ApplicationController
  after_action :set_referrer, unless: :user_signed_in?

  def set_referrer
    session[:referrer] = if request.post?
      request.fullpath + '/new'
    elsif request.patch? || request.put?
      request.fullpath + '/edit'
    else
      request.fullpath
    end
  end
end

class SessionController
  def create
    @user = User.find_by(params[:email])
                .try(:authenticate, params[:password])
    if @user
      render :new
    else
      redirect_to session[:referrer] || root_path
    end
  end
end

Post a Comment for "Navigation-based Redirections After Sign Up"