How to rescue OmniAuth::Strategies::OAuth2::CallbackError?

后端 未结 4 546
名媛妹妹
名媛妹妹 2021-01-30 13:23

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\' b

4条回答
  •  逝去的感伤
    2021-01-30 13:51

    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

提交回复
热议问题