DEV Community


Posted on

Redirecting users to their intended url via the session hash in rails after signing in.

As you have probably done before inside of a rails app, requiring users to be signed in before being able to access certain pages in the application is a pretty common task. Typically, you would have a method that runs as a before action inside of a controller.

So a user visits your app, tries to access say, their own profile page and since they didn't sign in first, the are kicked to the sign in page and hopefully politely told that they must sign in first. Pretty standard stuff here.

But depending on where you redirect the user to after they sign in, that my not align with where the user was trying to go to in the first place before being sent to the sign in screen.

Image this case, say you have a blogging application and a user visits the app, and immediately tries to edit a blog post they wrote, perhaps a friend pointed out a typo to them and they want to address the typo. So the user initially set out to land on a specific posts page so that they could then click an edit button to edit the post.

However, the user is redirected to the sign in screen as a result of a before action you have to require sign in. The user signs in successfully but is then routed to their profile page, which is not where the user originally wanted to go though. How can we provide a helping hand to the user so that we get them back to their original target of a specific posts page?

Our good friend the session hash is here to help us! We can modify our require sign in method to grab the intended url that the user was attempting to go to by using the request object that is passed along when a user navigates around the application. Then we can grab the url property out of the request object and store that inside the session hash for later use. So our require sign in method may look something like this:

  def require_signin
    unless current_user
      session[:intended_url] = request.url
      redirect_to new_session_url, alert: "Please sign in first!"
Enter fullscreen mode Exit fullscreen mode

Here we are saying that unless there is a current user we want to store that url in the session hash and redirect the user to the sign in page.

Now after the user signs in we can use the :intended_url property of the session hash, if it's there to get the user back to where they originally wanted to go. That could look something like this:

    def create
        if user = User.authenticate(params[:email_or_username], params[:password])
            session[:user_id] =
            flash[:notice] = "Welcome back, #{}!"
            redirect_to(session[:intended_url] || user)
            session[:intended_url] = nil
  [:alert] = "Invalid email/password combination!"
            render :new
Enter fullscreen mode Exit fullscreen mode

There is a bit going on here but the important line is the fourth line inside the create action. What happens here is that after successfully being authenticated, the user will be redirected to their intended url if that property is set or instead they will be redirected to their profile page.

Mission accomplished!

Happy Rubying!!!

Top comments (0)