7

Railscast 241 Simple OmniAuthに従おうとしています が、最後に Route Globbing がない限り、正常に動作します/config/routes.rb:

match '*uri' => "posts#index"

グロビングでリクエスト/auth/twitterすると、OmniAuth は何もしません:

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)

グロビングルートがなければ、正しく認証されます。

ルートグロビングと OmniAuth の両方を持つ方法はありますか?

4

2 に答える 2

17

OmniAuth プロセス/auth/:providerは、 URL が呼び出されたときに次の機能を提供することです。

  1. OmniAuth が存在しないかのように、基になる Rack/Rails アプリにリクエストを渡します。
  2. 基礎となるアプリケーションが 404 を生成したかどうかを判断します。
  3. 存在する場合は、実際の OmniAuth 機能を呼び出します。

ルートグロビングを使用して基本的にすべてを照合しているため、アプリケーションは決して 404 を返さず、OmniAuth はその仕事を行うことができません。当面の選択肢が 2 つあります。

OmniAuth ルートを 404 に手動で一致させる

次のように新しいルートを追加します。

match '/auth/:provider' => 'omniauth#passthru'

次に、404 を生成するコントローラーとアクションを作成します。

class OmniauthController < ApplicationController
  def passthru
    render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
  end
end

Glob ルートで 404 ステータスを確認する

あなたの glob ルートは何らかの形で URL に一致する投稿を検索すると思います。失敗しても (PostsController#index投稿が見つからない場合など)、404 を生成できます。

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
于 2011-04-04T02:23:56.653 に答える
0

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
于 2011-04-04T08:28:07.417 に答える