Search code examples
ruby-on-railsruby-on-rails-3routesomniauthglob

OmniAuth doesn't work with Route Globbing in Rails3


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?


Solution

  • 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