开发者

Omniauth - Origin is nil

开发者 https://www.devze.com 2023-02-28 15:45 出处:网络
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'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['omn开发者_运维问答iauth.origin'] right after callback, i get a nil object.

Thanks for your help!


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.


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.

0

精彩评论

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

关注公众号