15

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
Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
scud bomb
  • 415
  • 3
  • 19
  • similar problem with a few of my new apps...i've followed the steps, done this successfully in the past, but now just get no route matches error :/ – Danny May 02 '12 at 02:48
  • @picardo havent touched this in over five months, but specifying the Twitter callback as suggested below should take care of the issue. – scud bomb Jul 15 '12 at 20:00

11 Answers11

18

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).

TomDogg
  • 3,803
  • 5
  • 33
  • 60
10

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.

Community
  • 1
  • 1
likethesky
  • 846
  • 3
  • 12
  • 28
4

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

Justin D.
  • 4,946
  • 5
  • 36
  • 69
1

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

Dan Sandland
  • 7,095
  • 2
  • 29
  • 29
1

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.

Chris
  • 443
  • 8
  • 16
0

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.

C. Louis S.
  • 354
  • 3
  • 8
0

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
vladCovaliov
  • 4,333
  • 2
  • 43
  • 58
0

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.

Vince
  • 3,962
  • 3
  • 33
  • 58
0

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

Tim Kozak
  • 4,026
  • 39
  • 44
0

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
Basic
  • 26,321
  • 24
  • 115
  • 201
0
match '/auth/:provider/callback' => 'sessions#auth_callback', :as => :auth_callback

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

Richie Min
  • 654
  • 5
  • 15