scud bomb
scud bomb

Reputation: 415

OmniAuth routing error, no route match

I am working through Ryan Bates railscast #235 OmniAuth Part 1, using the OmniAuth gem to allow users to sign in to my web app using Twitter or Facebook and later Google Apps.

Right now I am encountering this error

Routing Error

No route matches [GET] "/auth/twitter"

I have correctly set up my routes.rb file to handle the auth callback provider match like so:

  match "/auth/:provider/callback" => "authentications#create"

When i link to localhost:3000/auth/twitter, i get this error. where as Bates in his Railscast at -07:36.

What could be a possible solution to this issue? Would it be an issue with routes.rb? or omniauth.rb?

Our omniauth.rb looks like this:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, 'OURCONSUMERKEY', 'OURCONSUMERSECRET'
  provider :twitter,  'OURCONSUMERKEY', 'OURCONSUMERSECRET'
end

Upvotes: 15

Views: 9394

Answers (11)

Tim Kozak
Tim Kozak

Reputation: 4182

Here is my fix to this problem:

If you proxying from Nginx:

location @rails {
    proxy_set_header Host $http_host;
    proxy_set_header Client-IP $remote_addr;
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass http://rails_app;
  }

Add an older version of omniauth to your Gemfile

#auth
gem 'omniauth-facebook', '~> 8.0'
gem 'omniauth', '~> 1.9.1' #this is important

Set force SSL

config.force_ssl = ENV['CLIENT_URL'].include?("https")

Related answer https://stackoverflow.com/a/66651142/788798

Upvotes: 0

Vince
Vince

Reputation: 4232

Add gem 'omniauth-twitter' to your Gemfile, re-run bundle, and restart your web server. Prior to Rails 4.0, I believe you have to add the gem line to an :assets group.

Upvotes: 0

vladCovaliov
vladCovaliov

Reputation: 4403

Be sure to add your 'omniauth.rb' in the config/initializers/*

My config/initializers/omniauth.rb looks like this

OmniAuth.config.logger = Rails.logger 

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :twitter, TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET
end

Upvotes: 0

Chris
Chris

Reputation: 449

I have seen the same problem when using omniauth 1.1.3 on Rails 2.3.16. It worked fine in development when running under webrick, but when running in production under Fastcgi the omniauth providers fail to detect any of the routes.

The problem was that the fastcgi code wasn't populating the PATH_INFO environmment variable correctly, and omniauth is depending on that.

The solution was to add another middleware to fix up PATH_INFO. I used this:

class Rack::PathInfoRewriter
  def initialize(app)
    @app = app
  end

  def call(env)
    env.delete('SCRIPT_NAME')
    parts = env['REQUEST_URI'].split('?')
    env['PATH_INFO'] ||= parts[0]
    env['QUERY_STRING'] ||= parts[1].to_s
    @app.call(env)
  end
end

Note the ||= it was necessary so that webrick continued to work OK in development mode.

Upvotes: 1

C. Louis S.
C. Louis S.

Reputation: 364

I had this same problem. The part that I was missing was putting the following in the Gem File

gem 'devise'

When I ran bundle install and refreshed the page it was fixed.

Upvotes: 0

likethesky
likethesky

Reputation: 846

fyi, if you're encountering this issue and you're combining Devise 2.1.x with OmniAuth 1.x and OAuth2, be aware that best practice now is to use /users/auth/facebook (that is, a directory in your controllers called, 'users/') ...

Accordingly, you'll need to hit /users/auth/facebook, even though almost all the tutorials, examples, and guides out there for OmniAuth say to hit /auth/facebook! This (in combination with the fact that Facebook wouldn't update my Site URL until I added the port # :3000, saved, propagated & hit it, then removed it again) had me stumped for a period of time that shall remain untold, to protect the chagrined. :-)

Also, unlike the answer with most votes right now--which of course solves the problem, but doesn't allow you to integrate with Devise--I didn't need to remove :omniauthable from Devise (once I was hitting the correct URL). It only 'causes conflicts' for me when I was using the wrong URL.

EDIT: Also, unlike in the original question, with Devise 2.1.x and OmniAuth 1.x, as far as I know, one doesn't need to create an omniauth.rb named initializer for Rack--with Devise, you just add your OmniAuth bits to config/initializers/devise.rb (but not 100% sure about this). See plataformatec/devise's OmniAuth Overview under Facebook example section at top, for more detail.

Upvotes: 10

TomDogg
TomDogg

Reputation: 3940

You need to comment out ':omniauthable' in your model used by the Devise gem (usually it's the model 'User' = the user.rb file):

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :recoverable,
         :rememberable, :trackable, :validatable # plus whatever other calls...
       # :omniauthable

  [...]

end

Using the ':omniauthable' call means loading devise/omniauth components (which cause conflicts with your omniauth setup).

Upvotes: 18

Dan Sandland
Dan Sandland

Reputation: 7193

Specifying the Callback URL for the app on Twitter should resolve this.

Upvotes: 1

Justin D.
Justin D.

Reputation: 4976

Actually, omniauth takes care of defining routes for twitter.

So adding this code is only for the callback

match "/auth/twitter/callback" => "sessions#create"
match "/signout" => "sessions#destroy", :as => :signout

Try restarting your server : rails server

Upvotes: 4

user1191500
user1191500

Reputation: 19

SB, May I make a suggestion? Look up episode #241 first. It is simpler episode. I tend to think your issue is NOT with routes.rb. I am using OmniAuth to authenticate users and to send tweets on behalf of my users and the only routes I have for that part is:

 match "/auth/twitter/callback" => "sessions#create"
 match "/signout" => "sessions#destroy", :as => :signout

Upvotes: 0

Richie Min
Richie Min

Reputation: 654

match '/auth/:provider/callback' => 'sessions#auth_callback', :as => :auth_callback

it works in my project, you can have a try like this

Upvotes: 0

Related Questions