问题
I am trying to follow the Railscast 241 Simple OmniAuth and it works fine unless I have Route Globbing at the end of /config/routes.rb
:
match '*uri' => "posts#index"
If I request /auth/twitter
with the globbing then OmniAuth does nothing:
Started GET "/auth/twitter" for 127.0.0.1 at 2011-04-03 19:17:44 +0200
Processing by PostsController#index as HTML
Parameters: {"uri"=>"auth/twitter"}
Rendered posts/index.html.haml within layouts/application (9.0ms)
Completed 200 OK in 103ms (Views: 14.6ms | ActiveRecord: 0.7ms)
Without the globbing route it authenticates correctly.
Is there a way to have both the route globbing and OmniAuth?
回答1:
The OmniAuth process is to provide the following functionality when a /auth/:provider
URL is called:
- Pass the request to the underlying Rack/Rails app as if OmniAuth wasn't there;
- Determine whether or not the underlying application generated a 404;
- If it did, invoke the actual OmniAuth functionality.
Since you are essentially matching everything using your route globbing, your application will never give 404's, and OmniAuth cannot do it's job. I see two immediate options.
Match OmniAuth Routes to a 404 Manually
Add a new route as follows:
match '/auth/:provider' => 'omniauth#passthru'
Then create a controller and action that generates a 404:
class OmniauthController < ApplicationController
def passthru
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
end
Determine 404 Status in the Glob Route
I assume that your glob route will search for a post matching the URL somehow; you can take misses (e.g. when PostsController#index
can't find a post) and generate 404's then.
class PostsController < ApplicationController
def index
if @posts = Post.find_by_current_url_or_whatever
render 'index'
else
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
end
end
回答2:
Slightly modified suggestion of Brandon Tilley:
# config/routes.rb
match '/auth/:provider/callback' => 'sessions#create'
match 'auth/*rest' => 'application#omniauth'
match '*uri' => 'posts#index'
# app/controllers/application_controller.rb
def omniauth
render text: 'Authentication', status: 404
end
来源:https://stackoverflow.com/questions/5531263/omniauth-doesnt-work-with-route-globbing-in-rails3