使用Devise和Omniauth的路由问题

4

我正在尝试让OAuth和Devise一起工作,但是当我尝试通过Facebook进行认证时,出现了Controller::RoutingError (No route matches "/users/auth/facebook/callback"):的错误。

奇怪的是,使用Google Apps时不会出现这个问题。(回调路由相同)

有什么想法吗?

回调控制器:

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"则不会?

你的文件是否在app/controllers/users/omniauth_callbacks_controller.rb中? - David
是的!就像我说的 - 谷歌的可以正常工作 - 只有在使用Facebook时出现问题。 - Ash
1个回答

8

找到了:

在 Devise 用户模型中,我添加了 :omniauth_providers => [:facebook, :openid, :google_apps] 模块,但是它(显然)没有做我以为的那样。我删除了这一行,现在我的所有认证逻辑再次正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接