Problem with Routes using Devise and Omniauth

2019-03-21 10:45发布

问题:

I am trying to get OAuth and Devise working together but I get Controller::RoutingError (No route matches "/users/auth/facebook/callback"): when trying to auth via Facebook.

Weirdly, the problem doesn't happen with Google Apps. (same callback route).

Any ideas?

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

Why would the :provider => "facebook" trigger a RoutingError, but not :provider => "google_apps"?

回答1:

Found it:

in the devise user model, i had the module: :omniauth_providers => [:facebook, :openid, :google_apps] added which (apparently) doesn't do what I thought it did. I removed the line and now all my auth logic works properly once again.