I’m using Devise and Omniauth for my login process. For some reason, I can access the route “users/auth/facebook” or “users/auth/twitter” just fine. But they don’t show up when I do rake routes, so I have no idea what the helper method to get these paths is (e.g. something_something_path). Can someone help me out?
I can’t show all of my routes, but I can say that the only route that matches “/users/auth/…” that’s showing up is this one (from rake routes):
user_omniauth_callback /users/auth/:action/callback(.:format) {:action=>/(?!)/, :controller=>"users/omniauth_callbacks"}
BTW, when I say I “can access the route just fine”, I mean this works (redirects me correctly to facebook or twitter):
<%= link_to "Connect", "users/auth/facebook" %>
Also, the routes should be the default Devise omniauth routes for the user model
With regard to why this doesn’t show up in
rake routes, first note how the task is implemented. It is part of railties, and it gets the routes to show as such:So we can see that it is asking the
Rails.applicationfor its routes.Next note that the Omniauth gem “is a flexible authentication system utilizing Rack middleware”.
Because it uses Rack middleware it does not ‘know’ anything about the
Rails.applicationused byrake routes, and so its routes don’t appear in that task.You can get a good introduction to Rack middleware in this Railcast.
Delving a little deeper we can see from
rake middlewarethatOmniAuth::Builderappears before your rails app in the stack. So how does it handle theauth/twitterroute?It does so by checking for a
request_pathin itscall, you can see the check here, and you can see how therequest_pathis built here (path_prefixisauthby default, andnamein your case istwitter.When using Omniauth with Devise, the
path_prefixis set automatically, as noted here.