Omniout - Origin zero

I use the gem with twitter. When calling back, check if the user exists and create it, or send it to the home page.
I might be doing something wrong, but in my callback code, request.env ['omniauth.origin'] is nil .

My code is pretty 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 return request.env ['omniauth.origin'] immediately after the callback, I get a nil object.

Thanks for your help!

+6
source share
2 answers

Try version 0.2.0. I was able to get this version to work without any problems with Rails 3.0.9 and Ruby 1.9.2-p180.

  • If your line above is not listed as

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

  • Regarding omniauth.origin nil , see this discussion .

0
source

You need to manually set the source in the link in your view. In Rails 4, request.original_url is the best way to do this.

In your Twitter link_to button, you can set the source manually:

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

Then in your SessionController when redirecting to request.env ['omniauth.origin'] it should work correctly.

0
source

Source: https://habr.com/ru/post/886712/


All Articles