使用 Devise 和 Omniauth 的路由问题

发布于 2024-11-15 10:33:55 字数 1682 浏览 1 评论 0原文

我试图让 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

paths.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”

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"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄情伤 2024-11-22 10:33:55

找到它:

在设计用户模型中,我有模块: :omniauth_providers => [:facebook, :openid, :google_apps] 添加了(显然)它没有做我想象的事情。我删除了该行,现在我的所有身份验证逻辑再次正常工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文