Question

How can I handle the error:

OmniAuth::Strategies::OAuth2::CallbackError

...generated by OmniAuth when a user goes through to the Facebook login, but decides to cancel?

I have seen a few threads about this, but none of the solutions worked for me.

I tried placing this:

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

...into the omniauth.rb initializer file with no success.

I am using the omniauth-facebook gem with Rails 4.0.2.

Any help greatly appreciated.

Many thanks! Michael.

My gemfile.lock file reveals the following gems related to the omniauth-facebook gem:

oauth2 (0.8.1)
  faraday (~> 0.8)
  httpauth (~> 0.1)
  jwt (~> 0.1.4)
  multi_json (~> 1.0)
  rack (~> 1.2)
omniauth (1.1.4)
  hashie (>= 1.2, < 3)
  rack
omniauth-facebook (1.5.1)
  omniauth-oauth2 (~> 1.1.0)
omniauth-oauth2 (1.1.1)
  oauth2 (~> 0.8.0)
  omniauth (~> 1.0)
Was it helpful?

Solution

I upgraded the omniauth-facebook gem to version 1.0.6 and now it is working as expected.

For anyone else coming across this issue and would like to capture this error, here's what you need:

/config/initializers/omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do

    provider :facebook, 'xxx', 'xxx', scope: "email,publish_stream,user_location,user_birthday"

end

/config/initializers/omniauth_failure_callback.rb

OmniAuth.config.on_failure = Proc.new do |env|

    UsersController.action(:omniauth_failure).call(env)

end

/app/controllers/users_controller.rb

def omniauth_failure

    flash[:danger] = "Unable to connect with Facebook at this time."
    redirect_to root_url

end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top