使用Devise和Omniauth的路由问题

时间:2011-06-14 05:40:52

标签: ruby-on-rails routes devise omniauth

我正在努力让OAuth和Devise一起工作,但在尝试通过Facebook进行身份验证时我得到Controller::RoutingError (No route matches "/users/auth/facebook/callback"):

奇怪的是,Google Apps不会出现此问题。 (同一回调路线)。

有什么想法吗?

callback_controller:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

  def facebook
    # You need to implement the method below in your model
    @user = User.find_for_facebook_oauth(env["omniauth.auth"], current_user)

    if @user.persisted?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
      sign_in_and_redirect @user, :event => :authentication
    else
      session["devise.facebook_data"] = env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end

  def google_apps
    @user = User.find_for_google_apps_oauth(env["omniauth.auth"], current_user)

    if @user.persisted?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google Apps"
      sign_in_and_redirect @user, :event => :authentication
    else
      session["devise.google_apps_data"] = env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end

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

end

routes.rb中:

devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } do
  get '/users/auth/:provider' => 'users/omniauth_callbacks#passthru'
end

为什么:provider => "facebook"会触发RoutingError,而不是:provider => "google_apps"

1 个答案:

答案 0 :(得分:8)

找到它:

在设计用户模型中,我添加了模块::omniauth_providers => [:facebook, :openid, :google_apps],其中(显然)没有按照我的想法做到。我删除了该行,现在我的所有auth逻辑再次正常工作。