Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 964
scud bomb Avatar asked Feb 05 '12 21:02

scud bomb


4 Answers

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

like image 109
TomDogg Avatar answered Oct 18 '22 19:10

TomDogg


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.

like image 36
likethesky Avatar answered Oct 18 '22 18:10

likethesky


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

like image 31
Justin D. Avatar answered Oct 18 '22 20:10

Justin D.


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

like image 23
Dan Sandland Avatar answered Oct 18 '22 20:10

Dan Sandland