Take the 2-minute tour ×

EDIT I've provided more detail on my objectives below.

I am having trouble deep linking users to my site after they log in through the landing page using the Omninauth gem.

The most common scenario is this: a user receives an email with a deep link to the site - say www.mysite.com/suggestions/15. We've placed a Facebook login (through Omniauth) on the landing page, (def landing page on the Authorization controller - see below). When a user tries to access any other page without being logged in they are bounced back via the authenticate_user! helper method placed in the relevant controllers. authenticate_user is defined in the Application Controller -see below.

Once they get bounced back they click a "log in with facebook" button on the landing page and we create a session for the user through def authcreate. This works in the classic omniauth way, i.e. a callback from facebook which captures the session token. Now at this point I want to redirect the user to the page they were trying to get to (e.g. www.mysite.com/suggestions/15) but instead they only go through the default page (www.mysite.com/suggestions). This is not a problem when the user is already logged in, but never works when they are logged out.

You can ignore authorize! as that is a separate module (confusing I know) which is concerned with admin rights. Ditto on check_authorization.

I've created a module and placed it into lib/

module RedirectModule

def self.included(controller)
controller.send :helper_method, :redirect_to_target_or_default

def redirect_to_target_or_default(default, *args)    
    redirect_to(session[:return_to] || default, *args)
    session[:return_to] = nil

def redirect_to_or_default(target, *args)
    redirect_to(target || default, *args)


def store_target_location # Friendly redirect: store URL.
    session[:return_to] = request.url 

There is an include in the ApplicationController:

class ApplicationController < ActionController::Base

  include RedirectModule

def current_user
  if session[:user_id]

helper_method :current_user

def authenticate_user!
  if !current_user
redirect_to "/"

I initiate the sessions on the following controller:

class AuthorizationController < ApplicationController

  def landingpage
   authorize! :auth, :landingpage

    if current_user




  def authcreate
    authorize! :auth, :authcreate
    authHash = env["omniauth.auth"]
    existingUser = User.where(authHash.slice(:provider, :uid)).first
    user = User.from_omniauth(authHash)
    session[:user_id] = user.id


  def authdestroy
    authorize! :auth, :authdestroy
    session[:user_id] = nil
    redirect_to "/"


What am I doing wrong?

share|improve this question
Perhaps I am missing something, but when the session ends (user closes the browser) or when you delete the session, your authenticate_user! method redirects them to /. But a number of other methods that would seem to be important are not included. It would be helpful if you could remove the non-relevant code, and include the relevant code. Also, it really looks like you're reinventing the wheel here -- there are several really good approaches to handling this case, including the Devise (gem). –  Tom Harrison Jr Dec 5 '12 at 22:43
Ok, I've edited it but there isn't that much to edit. At this point I don't want to migrate to Devise although that may be an option in the future. Could you elaborate on what you mean by "But a number of other methods that would seem to be important are not included"? –  mytwocentsisworthtwocents Dec 5 '12 at 22:49
An important edit was that you added a call to store_target_location in authenticate_user!. Not sure how authorize! and reset_session come into play. Where is check_authorization defined? And there's some OAuth stuff. At a broader level, I think the question needs a description of what you want to happen (versus what is happening) -- that part isn't clear to me, at least. –  Tom Harrison Jr Dec 6 '12 at 15:04
I've been looking so long at this code I have a forest for the trees problem, so thanks for pulling me back. I've added more details to the question above, but let me know if I should add anything else. –  mytwocentsisworthtwocents Dec 6 '12 at 15:38
sorry, just added now! –  mytwocentsisworthtwocents Dec 6 '12 at 15:44

Your Answer



Browse other questions tagged or ask your own question.