3

I have implemented devise and omniauth-facebook following the instructions below:

https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview

Problem

My "facebook" callback function does get called.

If I uncomment the "facebook" function, error occurs. Routing seems to be working fine

However, the code inside the function does not seem to be executed.

The following code does not raise any error.

I have been looking in to this problem for days. Please help...

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController


  def facebook
    raise params.inspect
  end

  def failure
    redirect_to root_path
  end

end

Log

Finished "/cable/" [WebSocket] for ::1 at 2016-09-10 22:32:32 +0900
MessagesChannel stopped streaming from messages
Started GET "/cable" for ::1 at 2016-09-10 22:32:32 +0900
Started GET "/cable/" [WebSocket] for ::1 at 2016-09-10 22:32:32 +0900
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
MessagesChannel is transmitting the subscription confirmation
MessagesChannel is streaming from messages
Started GET "/users/auth/facebook" for ::1 at 2016-09-10 22:32:34 +0900
I, [2016-09-10T22:32:34.770050 #84008]  INFO -- omniauth: (facebook) Request phase initiated.
Started GET "/users/auth/facebook" for ::1 at 2016-09-10 22:32:34 +0900
I, [2016-09-10T22:32:34.994734 #84008]  INFO -- omniauth: (facebook) Request phase initiated.
Finished "/cable/" [WebSocket] for ::1 at 2016-09-10 22:32:35 +0900
MessagesChannel stopped streaming from messages
Started GET "/users/auth/facebook/callback?code=AQBICSjBfjCN7rGbCZLSkdg25FqTZGsnDrJi1UhBj0RwSQBjuZ5bTxEA025jApkwWiianigtILjRV5Uv067Yg73MGzi7sB5BT9yU0kjm7wzYzkBhWMmT0Aecw4ajACkSbBNfVUIii0cokommOAbSJgbzmfKRgbMGmdgYZsF6rBDuPyAGHnFgAa6bSl3jUmzW25SCTY9CDARiGlr880B-gwMs3gX0_KbtXnygAkBhNHoBSFFOIIY7w4QIMQHzZe1aLz6VWz_LxnuN88Ao8_lWOLPCfxsOWUJkdjYbLCowXmu1bpOu1zUuXSf6Dw9qBnWm73XvMyMD3iSasRRqlFW1uVde&state=2362c719c103b6b19f053ccc31b0b90ab34d137a71c2cb8d" for ::1 at 2016-09-10 22:32:40 +0900
I, [2016-09-10T22:32:40.549353 #84008]  INFO -- omniauth: (facebook) Callback phase initiated.
Processing by Users::OmniauthCallbacksController#facebook as HTML
  Parameters: {"code"=>"AQBICSjBfjCN7rGbCZLSkdg25FqTZGsnDrJi1UhBj0RwSQBjuZ5bTxEA025jApkwWiianigtILjRV5Uv067Yg73MGzi7sB5BT9yU0kjm7wzYzkBhWMmT0Aecw4ajACkSbBNfVUIii0cokommOAbSJgbzmfKRgbMGmdgYZsF6rBDuPyAGHnFgAa6bSl3jUmzW25SCTY9CDARiGlr880B-gwMs3gX0_KbtXnygAkBhNHoBSFFOIIY7w4QIMQHzZe1aLz6VWz_LxnuN88Ao8_lWOLPCfxsOWUJkdjYbLCowXmu1bpOu1zUuXSf6Dw9qBnWm73XvMyMD3iSasRRqlFW1uVde", "state"=>"2362c719c103b6b19f053ccc31b0b90ab34d137a71c2cb8d"}
Redirected to http://localhost:3000/
Filter chain halted as :authenticate_user! rendered or redirected
Completed 302 Found in 1ms (ActiveRecord: 0.0ms)

Versions

devise 4.2.0
omniauth 1.3.1
omniauth-oauth2 1.4.0
omniauth-facebook 4.0.0
rails 5.0.0.1
Oscar
  • 31
  • 1

2 Answers2

1

I used the callbacks in a little bit different way: you should declared the callback in the omniauth.rb file:

Rails.application.config.middleware.use OmniAuth::Builder do
    provider :facebook, "app_id", "password",
        scope: 'public_profile', info_fields: 'id,name,link', display: 'popup', callback_path: '/stores/facebook_callback'

end

and of course add 'facebook_callback' action in the controller (my case it was stores), and also add it to the routes file.

Guy Dubrovski
  • 1,542
  • 1
  • 20
  • 25
0

It might be that some other authentication mechanism is interfering with the omniauth flow and preventing the callback from being executed.

You could check your ApplicationController for before_action statements that might cause the problem and add them as a skip_before_action in the OmniauthCallbacksController.

E.g. if you have:

class ApplicationController < ActionController::Base
  before_action :authenticate_user
  ...

Then add:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  skip_before_action :authenticate_user
  ...

It fixed it for me when I had the exact same problem.

jox
  • 2,218
  • 22
  • 32