2

我将用户发送到 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

路线.rb

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

1 回答 1

2

由于您没有使用纯 OmniAuth,但您也在使用 Devise,所以情况有些不同:

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

    match '/users/auth/:provider/callback' => 'authentications#create'

  2. 设计自动调用名称与提供者匹配的方法。所以如果你用 Twitter 登录,你应该有一个twitter方法。如果您使用 Facebook 登录,您应该有一个facebook方法。

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

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

这样,您不必创建twitterfacebook方法。

于 2013-10-10T17:38:27.470 回答