0

There are some pages in my app which require authentication with linked in. I use before filter to redirect user to /auth/linked_in if s/he is not already authenticated. I want that after authentication user should be redirected to page which required authentication instead of page from which user came from.

Before filter looks like:

def require_linked_in!
  unless current_user and current_user.linked_in_authenticated?
    redirect_to "/auth/#{Authentication::LINKED_IN}"
  end
end

In my omniauth callback I do some stuff, like logging in or associating provider data with user account and if everything was ok, redirect_to request.env['omniauth.origin'] || root_url, but this redirects back the user to page from which he clicked the link to page requiring linked_in authentication and not the target page.

How can I redirect the user to page which triggered authentication instead of page which user came from?

PS: User can only sign up using the registration form for the app, but can later on connect there linked and facebook profile and also use them instead of normal email/password login form. If it makes any difference.

rubish
  • 10,887
  • 3
  • 43
  • 57

1 Answers1

3

I guess you need to store the url in your session and redirect after the omniauth callback.

def require_linked_in!
  session[:return_to] = request.url
  unless current_user and current_user.linked_in_authenticated?
    redirect_to "/auth/#{Authentication::LINKED_IN}"
  end
end

In your callback-action you can use:

redirect_to session[:return_to] || root_url

Hope it helps!

Leigh
  • 28,765
  • 10
  • 55
  • 103
marcus3006
  • 365
  • 3
  • 12