Omniauth - 起源为零
我正在将 gem 与 Twitter 一起使用。在回调时,检查用户是否存在并创建他或将他发送回主页。
我可能做错了什么,但在我的回调代码中,request.env['omniauth.origin'] 为零。
我的代码非常简单:
whatever.html.erb
<%= link_to image_tag("twitter-connect.png"), "/auth/twitter" %>
routes.rb
match "/auth/:provider/callback" => "sessions#create"
sessions_controller.rb
def create
auth = request.env["omniauth.auth"]
user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
if !user.email
redirect_to confirm_path, :notice => "Add your email!"
else
redirect_to request.env['omniauth.origin'] || root_url, :notice => "Signed in!"
end
end
如果我提出request.env['omniauth.origin']nil 对象。
感谢您的帮助!
I'm using the gem with twitter. On callback, check if the user exists and create him or send him back to the homepage.
I might be doing something wrong, but in my callback code, request.env['omniauth.origin'] is nil.
My code is quite simple :
whatever.html.erb
<%= link_to image_tag("twitter-connect.png"), "/auth/twitter" %>
routes.rb
match "/auth/:provider/callback" => "sessions#create"
sessions_controller.rb
def create
auth = request.env["omniauth.auth"]
user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
if !user.email
redirect_to confirm_path, :notice => "Add your email!"
else
redirect_to request.env['omniauth.origin'] || root_url, :notice => "Signed in!"
end
end
If I raise request.env['omniauth.origin'] right after callback, i get a nil object.
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试版本 0.2.0 - 我已经能够让这个版本在 Rails 3.0.9 和 Ruby 1.9.2-p180 上正常工作,没有任何问题。
上面的行不应该读作
user = auth.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
至于
omniauth.origin
为nil
,请参见 此讨论。Try version 0.2.0 - I've been able to get this version to work without any issues with Rails 3.0.9 and Ruby 1.9.2-p180.
Shouldn't your line above read as
user = auth.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
As for
omniauth.origin
beingnil
, see this discussion.您需要在视图中的链接中手动设置原点。在 Rails 4 中,request.original_url 是实现此目的的最佳方法。
在 link_to Twitter 按钮中,您可以手动设置原点:
然后在 SessionsController 中,当您重定向到 request.env['omniauth.origin'] 时,它应该可以正常运行。
You need to manually set the origin in the link in your view. In Rails 4, request.original_url is the best way to do this.
In your link_to Twitter button, you can set the origin manually:
Then in your SessionsController when you redirect to request.env['omniauth.origin'], it should function correctly.