使用 facebook 等 omniauth 提供程序时,设计无法正确重定向到存储位置 [英] Devise doesn't redirect properly to stored location when using omniauth provider like facebook

查看:12
本文介绍了使用 facebook 等 omniauth 提供程序时,设计无法正确重定向到存储位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中有一些受保护且需要用户身份验证的操作.由于我使用的是 devise,所以我使用了 authentication_user!在过滤之前保护它们.每当用户点击受保护页面时,设计要求用户登录,然后重定向回受保护页面.这部分工作得很好.

I have in my app some actions which are protected and need user authentication. Since I'm using devise, I use authenticate_user! before filter to protect them. Whenever user hits the protected page, devise ask the user to login and then redirects back to the protected page. This part works perfectly.

问题是,当用户尝试通过我的应用登录 Facebook 时,设备在登录后不会将用户重定向到受保护的页面.它总是将用户扔回 root url.使用标准身份验证,这不是问题

The problem is that when user tries to login with Facebook through my app, devise doesn't redirect the user to the protected page after login. It always throw the user back to root url. With standard authentication this is not a problem

我怀疑它与设计的 passthru 方法有关 - omniauth 集成需要.任何帮助将不胜感激

I am suspecting it has something to do with passthru method which devise - omniauth integration requires. Any help would be greatly appreciated

这是我的 omniauth 回调代码片段:

Here is my code snippet for omniauth callback:

def facebook
# You need to implement the method below in your model
omniauth = request.env["omniauth.auth"]
@user = User.find_for_facebook_oauth(omniauth, current_user)

if @user.persisted?
  flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
  sign_in_and_redirect @user, :event => :authentication
else
  session["devise.facebook_data"] = env["omniauth.auth"]
  redirect_to new_user_registration_url
end
end

def passthru
  render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end

推荐答案

我们使用 Facebook JS 获取访问代码,然后返回到omn​​iauth_callback_controller.rb 并让用户登录.

We use Facebook JS to get the access code which then returns back to the omniauth_callback_controller.rb and signs the user in.

我发现原始 URL 保存在请求中.为我们服务.

I found that the origin URL is saved in the request. Worked a treat for us.

application_controller.rb

def after_sign_in_path_for(resource_or_scope)
   if request.env['omniauth.origin']
      request.env['omniauth.origin']
    end
end

这篇关于使用 facebook 等 omniauth 提供程序时,设计无法正确重定向到存储位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆