开发者

OmniAuth: redirect to page which requires authentication instead of the one which user came from

开发者 https://www.devze.com 2023-04-09 12:06 出处:网络
There are some pages in my app which require authentication with linked in. I use before filter t开发者_C百科o redirect user to /auth/linked_in if s/he is not already authenticated. I want that after

There are some pages in my app which require authentication with linked in. I use before filter t开发者_C百科o 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.


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号