OmniAuth doesn't work with Route Globbing in Rails3

后端 未结 2 592
孤独总比滥情好
孤独总比滥情好 2020-12-29 00:49

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 \         


        
2条回答
  •  粉色の甜心
    2020-12-29 01:35

    The OmniAuth process is to provide the following functionality when a /auth/:provider URL is called:

    1. Pass the request to the underlying Rack/Rails app as if OmniAuth wasn't there;
    2. Determine whether or not the underlying application generated a 404;
    3. 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
    

提交回复
热议问题