OmniAuth doesn't work with Route Globbing in Rails3

蓝咒 提交于 2019-11-30 04:03:50

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
Andrei

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!