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

Soundar Rathinasamy picture Soundar Rathinasamy · May 24, 2012 · Viewed 15.4k times · Source

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 :)

Answer

Peter P. picture Peter P. · Jun 29, 2012

You can set the on_failure proc in the omniauth initializer in an even cleaner fashion:

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