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

后端 未结 4 458
名媛妹妹
名媛妹妹 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:54

    There's a configuration to use /auth/failure instead of raising an error.

    I use OmniAuth 1.2.2 and when I checking the FailureEndpoint I found the code is like this:

    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:

    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:

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

提交回复
热议问题