【问题标题】:Omniauth/Devise, authenticating with Twitter works but callback shows failureOmniauth/Devise,使用 Twitter 进行身份验证有效,但回调显示失败
【发布时间】:2013-10-10 12:49:41
【问题描述】:

我将用户发送到 Twitter 以授权我的应用程序使用他们的帐户。

如果用户单击登录并正确验证,则他们将被发送到失败方法。我仍然可以通过返回的哈希访问他们的oauth_token 和oauth_token_secret。 (我觉得很奇怪?)

无论用户点击登录还是取消,它们总是被发送回失败方法。

为什么他们在正确验证时没有被发送到成功方法?

在我的路由文件中我尝试使用

devise_for :users, :controllers => { :registrations => 'registrations',
                                     :omniauth_callbacks => "authentications" }

但它从不调用 create 方法,总是调用 failure 方法。

我做错了什么?

编辑: authentications_controller.rb

class AuthenticationsController < Devise::OmniauthCallbacksController

  def create
    render :text => 'yes'
  end

  def failure
    render :text => request.env['omniauth.auth']
  end

  alias_method :twitter, :create
end

routes.rb

MyApp::Application.routes.draw do
  resources :authentications
  devise_for :users, :controllers => { :registrations => 'registrations', 
                                       :omniauth_callbacks => "authentications" }
  resources :users
end

【问题讨论】:

    标签: ruby-on-rails ruby devise omniauth twitter-oauth


    【解决方案1】:

    由于您使用的不是纯 OmniAuth,而是 Devise,所以情况有些不同:

    1. 您不需要定义以下路由:

      match '/users/auth/:provider/callback' =&gt; 'authentications#create'

    2. Devise 会自动调用名称与提供者匹配的方法。因此,如果您使用 Twitter 登录,您应该有一个 twitter 方法。如果您使用 Facebook 登录,您应该有一个 facebook 方法。

    如果您不希望这种区别,并且希望将所有身份验证定向到相同的方法(例如:create),那么您可以在 end 的控制器:

    # authentications_controller.rb
    alias_method :facebook, :create
    alias_method :twitter, :create
    

    这样,您不必创建twitter 和facebook 方法。

    【讨论】:

    • 我尝试放置 alias_method 但现在我收到错误“未定义的方法create' for class AuthenticationsController”。我的控制器中肯定有这种方法:s。我编辑了问题以包含控制器。
    • 我特别说“在控制器末尾添加alias_method”。如果你在开头添加它,在定义方法之前,那么它会抛出一个错误,说它是未定义的。
    • 如果你使用alias_method,你也可以去掉那个twitter方法。
    • 啊,是的,我把它移到了底部,但它仍然每次都调用失败方法:/。但仍然返回成功时您期望的哈希:s。我再次用我的控制器当前的样子更新了问题
    • 好吧,这确实很奇怪。您要么有另一条路线正在产生某种冲突,要么您需要更新您的 Devise gem。你的设计版本是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多