使用omniauth提供商(如Facebook)时,Devise不会正确重定向到存储的位置 [英] Devise doesn't redirect properly to stored location when using omniauth provider like facebook

查看:74
本文介绍了使用omniauth提供商(如Facebook)时,Devise不会正确重定向到存储的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中有一些被保护并需要用户认证的操作。由于我使用devise,我使用authenticate_user!之前过滤保护他们。每当用户点击受保护的页面时,设计者要求用户登录,然后重定向回到受保护的页面。这个部分工作完美。



问题是当用户尝试通过我的应用程序登录Facebook时,devise不会在登录后将用户重定向到受保护的页面。它总是将用户返回到根URL。使用标准认证这不是一个问题



我怀疑它与passthru方法有关 - 设计 - 全方位整合需要。任何帮助将不胜感激



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

  def facebook 
#您需要在模型中实现以下方法
omniauth = request.env [omniauth.auth]
@user = User.find_for_facebook_oauth(omniauth, current_user)

如果@ user.persisted?
flash [:notice] = I18n.tdevise.omniauth_callbacks.success,:kind => Facebook
sign_in_and_redirect @user,:event => :认证
else
会话[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获取访问代码,然后返回到
omniauth_callback_controller.rb并对用户进行签名。



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



application_controller.rb

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


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.

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

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

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

解决方案

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

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

in application_controller.rb

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

这篇关于使用omniauth提供商(如Facebook)时,Devise不会正确重定向到存储的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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