我将用户发送到 Twitter,以授权我的应用程序使用他们的帐户。
如果用户单击“登录”并正确进行身份验证,则他们将被发送到失败方法。我仍然可以通过返回的哈希访问他们的 oauth_token
和 oauth_token_secret
。 (我觉得很奇怪?)
无论用户单击登录还是取消,他们总是会被发送回失败方法。
当他们验证正确时,为什么他们没有被发送到成功方法?
在我的路线文件中,我尝试使用
devise_for :users, :controllers => { :registrations => 'registrations',
:omniauth_callbacks => "authentications" }
但它永远不会调用创建方法,总是调用失败方法。
我做错了什么?
编辑: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
最佳答案
由于您没有使用纯 OmniAuth,但也使用 Devise,所以情况略有不同:
您不需要定义以下路由:
匹配 '/users/auth/:provider/callback' => 'authentications#create'
Devise 会自动调用名称与提供者匹配的方法。因此,如果您使用 Twitter 登录,则应该有一个
twitter
方法。如果您使用 Facebook 登录,则应该有一个facebook
方法。
如果您不希望这种区别,并且希望所有身份验证都定向到同一方法(例如:create
),那么您可以在末尾添加以下内容 您的 Controller :
# authentications_controller.rb
alias_method :facebook, :create
alias_method :twitter, :create
这样,您就不必创建 twitter
和 facebook
方法。
关于ruby-on-rails - Omniauth/Devise,使用 Twitter 进行身份验证有效,但回调显示失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19296213/