Omniauth - 起源为零

发布于 2024-11-03 04:24:30 字数 946 浏览 2 评论 0原文

我正在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌路终见情 2024-11-10 04:24:30

尝试版本 0.2.0 - 我已经能够让这个版本在 Rails 3.0.9 和 Ruby 1.9.2-p180 上正常工作,没有任何问题。

  1. 上面的行不应该读作

    user = auth.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)

  2. 至于 omniauth.originnil,请参见 此讨论

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.

  1. Shouldn't your line above read as

    user = auth.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)

  2. As for omniauth.origin being nil, see this discussion.

于我来说 2024-11-10 04:24:30

您需要在视图中的链接中手动设置原点。在 Rails 4 中,request.original_url 是实现此目的的最佳方法。

在 link_to Twitter 按钮中,您可以手动设置原点:

"/auth/twitter?origin=#{request.original_url}"

然后在 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:

"/auth/twitter?origin=#{request.original_url}"

Then in your SessionsController when you redirect to request.env['omniauth.origin'], it should function correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文