简体   繁体   English

如何解救OmniAuth :: Strategies :: OAuth2 :: CallbackError?

[英]How to rescue OmniAuth::Strategies::OAuth2::CallbackError?

I am building a Rails application with Omniauth for log in service.To authenticate Google I am using OmniAuth Google OAuth2 Strategy . 我正在使用Omniauth构建Rails应用程序以进行登录服务。要对Google进行身份验证,我使用的是OmniAuth Google OAuth2策略

When user clicks 'allow access' button everything works fine.But when user clicks 'no thanks' button the below error is raised. 当用户点击“允许访问”按钮时,一切正常。但是当用户点击“不再感谢”按钮时,会出现以下错误。

OmniAuth::Strategies::OAuth2::CallbackError

I have tried adding the below rescue code in application controller. 我尝试在应用程序控制器中添加以下救援代码。

class ApplicationController < ActionController::Base
  rescue_from OmniAuth::Strategies::OAuth2::CallbackError, :with =>
    :omniauth_callback_error_handler

 protected

 def omniauth_callback_error_handler
  redirect_to init_sign_in_users_path
 end
end

But no luck. 但没有运气。

Any idea? 任何的想法?

Thank you :) 谢谢 :)

您可以以更清晰的方式在omniauth初始化程序中设置on_failure proc:

OmniAuth.config.on_failure = UsersController.action(:oauth_failure)

This happens because the authentication happens in a middleware so your controller is not involved in it. 发生这种情况是因为身份验证发生在中间件中,因此您的控制器不会参与其中。 This is where the exception is raised and the called code is this 这是引发异常的地方,被调用的代码就是这个

I think you can handle this kind of error by defining a callback in Omniauth initializer with this kind of code 我认为你可以通过使用这种代码在Omniauth初始化程序中定义回调来处理这种错误

Omniauth.config do |config|
  config.on_failure do
    # your handling code invoked in the context of a rack app
  end
end

Otherwise there is a commit of three months ago which introduce this behavior 否则会有三个月前的提交引入此行为

def redirect_to_failure
  message_key = env['omniauth.error.type']
  new_path = "#{env['SCRIPT_NAME']}#{OmniAuth.config.path_prefix}/failure?message=#{message_key}"
  Rack::Response.new(["302 Moved"], 302, 'Location' => new_path).finish
end

which states that on errors your user is redirected to /auth/failure with an error message, so you should be able to define a route for that path and handle it in your app. 这表明错误会导致用户被重定向到/auth/failure并显示错误消息,因此您应该能够为该路径定义路径并在应用中处理它。 Keep in mind that this won't happen in development mode so you need to try it in other envs. 请记住,这不会发生在开发模式中,因此您需要在其他环境中尝试它。 If this doesn't happen in production try to upgrade your omniauth gem to version 1.1.0 如果在生产中没有发生这种情况,请尝试将omniauth gem升级到1.1.0版

I have solved this problem with the Fabio's first suggestion. 我用法比奥的第一个建议解决了这个问题。

OmniAuth.config.on_failure = Proc.new do |env|
  UsersController.action(:omniauth_failure).call(env)
  #this will invoke the omniauth_failure action in UsersController.
end

In my UsersController 在我的UsersController中

class UsersController < ActionController::Base
  def omniauth_failure
    redirect_to init_sign_in_users_path
    #redirect wherever you want.
  end
end

There's a configuration to use /auth/failure instead of raising an error. 有一个配置使用/auth/failure而不是引发错误。

I use OmniAuth 1.2.2 and when I checking the FailureEndpoint I found the code is like this : 我用OmniAuth 1.2.2,当我检查FailureEndpoint我发现的代码是像这样

def call
  raise_out! if OmniAuth.config.failure_raise_out_environments.include?(ENV['RACK_ENV'].to_s)
  redirect_to_failure
end

And the failure_raise_out_environments is defined here : failure_raise_out_environments 在这里定义:

def self.defaults
  @defaults ||= {
    # other configurations
    :failure_raise_out_environments => ['development']
  }
end

The environment can be configured so the solution is easy. 可以配置环境以便解决方案。 I use Rails so I put below code in an initializer file: 我使用Rails,所以我将下面的代码放在初始化文件中:

OmniAuth.configure do |config|
  # Always use /auth/failure in any environment
  config.failure_raise_out_environments = []
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM