问题描述
我正在使用登录服务来构建Rails应用程序。要验证Google,我使用的是。
当用户点击允许访问按钮一切正常但是当用户点击不感谢按钮时,会出现以下错误。
OmniAuth :: Strategies :: OAuth2 :: CallbackError
我已经尝试在应用程序控制器中添加以下救援码。
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
$ c $
任何想法?
谢谢:)
解决方案这是因为身份验证发生在中间件,参与它。这是,调用代码为
我想你可以通过使用这种代码在Omniauth初始化程序中定义一个回调来处理这种错误。
Omniauth .config do | config |
config.on_failure do
#在应用程序环境中调用的处理代码
end
end
否则,三个月前有引入此行为
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], 'location'=> new_path).finish
end
用户被重定向到 / auth / failure
并显示错误消息,因此您应该能够为该路径定义路由并在应用程序中处理它。请注意,这不会发生在开发模式,因此您需要尝试在其他envs。如果这在生产环境中不会发生,请尝试将omniauth gem升级到版本1.1.0
I am building a Rails application with Omniauth for log in service.To authenticate Google I am using OmniAuth Google OAuth2 Strategy.
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 :)
解决方案 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.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. 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 :: Strategies :: OAuth2 :: CallbackError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!