Omniauth - Origin is 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['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.
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.
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.
精彩评论