2

I have struggled with this for 2 days now:

I have Omniauth-factbook implemented correctly with Devise. Now, I want to improve it by making the Facebook authentication occurs in the popup window instead of going to facebook.com. I followed Ryan's RailsCast and add display => 'popup' to my provider configuration in omniauth.rb

I successfully load login to facebook and got returned a hash request.authRequest, which contains information about userId.

However, when I got back to callback path (/auth/facebook/callback), I didn't get :provider and :uid from request.evn[omniauth.auth] like I used to.

Why omniauth hash was not created even when Facebook API returned the authentication hash? What can I do to fix this issue?

My controller to handle facebook callback is simple:

class ServicesController < ApplicationController
  def create
    auth = request.env["omniauth.auth"]
    debugger

Right at the debugger, when I use IRB to check, there's no omniauth hash in request.env.

BTW, I think I turned the cookie on in my coffeescript file:

jQuery ->
 $('body').prepend('<div id="fb-root"></div>')

  $.ajax
    url: "#{window.location.protocol}//connect.facebook.net/en_US/all.js"
    dataType: 'script'
    cache: true

window.fbAsyncInit = ->
  FB.init(appId: '<%= "myAppID" %>', cookie: true)

  $('#facebook_signin').click (e) ->
    e.preventDefault()
    FB.login (response) ->
      window.location = '/auth/facebook/callback' if response.authResponse

  $('#facebook_signout').click (e) ->
    FB.getLoginStatus (response) ->
      FB.logout() if response.authResponse
    true

Thank you.

Update: my omniauth.rb looks like this:

Rails.application.config.middleware.use OmniAuth::Builder do
  # The following is for facebook
  provider :linkedin, '3yn', 'iMJ'
  provider :twitter, 'SQ', 'T5fo'
  provider :facebook, '081', 'e80',
           scope: "email"
end
AdamNYC
  • 19,887
  • 29
  • 98
  • 154
  • You've got it set up as `/app/assets/javascripts/services.js.coffee.erb`? I'm trying to make sure I understand `'<%= "myAppID" %>'`. Seems like one too many sets of quote marks. – Austin Mullins Mar 01 '13 at 04:54
  • Hi Austin? I am not sure I understand your question, but I did have a file called facebook.js.coffee.erb that contained the coffeescript code mentioned in my question. In there, I have actual ID instead of myAppID, like `'<%="12345" %>'`. The first quotation mark is for injecting html element, and the second is to wrap a text string. – AdamNYC Mar 01 '13 at 15:18
  • If you have it hard-coded like that, why do you need it in an ERB block? – Austin Mullins Mar 01 '13 at 15:53
  • Austin: I don't need it. Just leave it there so that I will remember to change it to ENV[myAppID] later. I just removed ERB block and hard-coded my ID, but nothing changes. – AdamNYC Mar 01 '13 at 16:27
  • Ok, thanks for explaining that. It'd be nice if everything were as simple as that. – Austin Mullins Mar 01 '13 at 17:00

2 Answers2

0

(I have to answer rather than comment while I'm new-ish here.)

You appear to be mixing up a few things. Specifying :display => 'popup' does not trigger a popup window. It is a Facebook-related option that specifies the display mode of the login form (explained here: https://developers.facebook.com/docs/reference/dialogs/oauth/)

Turn omniauth facebook login into a popup explains some of what's going on, but you probably don't want to maintain your own popup window. Facebook's JavaScript SDK does this with the FB.login function (explained here: https://developers.facebook.com/docs/reference/javascript/FB.login/)

It's difficult to troubleshoot without all relevant code posted. From what you wrote, it sounds like the JavaScript side is working correctly. Out of curiosity, what does your omniauth.rb initializer look like?

Community
  • 1
  • 1
chrislopresto
  • 1,598
  • 1
  • 10
  • 22
  • Thanks for pointing out `:display =>'popup'` issue. Removing it doesn't help though. I was trying it out of desperation. I'll paste my omniauth.rb to my question. – AdamNYC Mar 06 '13 at 01:31
0

i did some tests and read comments abouts the railscast and you need to downgrade the gem

gem 'omniauth-facebook', '1.4.0'

If you need more information about it :

http://railscasts.com/episodes/360-facebook-authentication?view=comments#comment_159418

here the code : https://github.com/senayar/facebook_connect

hope it helps :)

rbinsztock
  • 3,025
  • 2
  • 21
  • 34
  • Hi senayar: thanks for trying to help. I tried to downgrade `omniauth-facebook` to 1.4.0 before (following the comment), but it didn't work for me. Could you kindly share the code on github? I would greatly appreciate it, as I spend days trying to sort this out. – AdamNYC Mar 06 '13 at 01:29
  • added with the scope for user email. – rbinsztock Mar 06 '13 at 02:06
  • Thanks a lot, senayar. I am actually using 1.4.0 now. I tried your code on github, but it doesn't work either. Disappointingly, I didn't get any error message either (so it is different from the comment on railscasts). I'll try your code again tomorrow. Did you successfully implement javascript version of omniauth-facebook with Devise? What did you try to help me to change? – AdamNYC Mar 06 '13 at 04:29
  • yes my version work with coffeescript and a popup. Clone it and try it :) you will see – rbinsztock Mar 06 '13 at 09:47
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25707/discussion-between-senayar-and-adamnyc) – rbinsztock Mar 06 '13 at 16:02