views:

412

answers:

1

I'm using the Facebook JS SDK for single-sign with my rails app. I translated the php code from the Facebook example (at the bottom of the page under "Single Sign-on with the JavaScript SDK") into ruby.

This appeared to be working great but I've noticed one user that gets continual redirects when trying to login. The logs look like:

Processing UsersController#login (for X.X.X.X at 2010-05-22 17:25:55) [GET]
Redirected to http://myapp.com/
Completed in 0ms (DB: 0) | 302 Found [http://myapp.com/login]

(times as many entries as my unfortunate user leaves the browser redirecting in a loop).

My client side code includes a callback with the "auth.sessionChange":

FB.Event.subscribe('auth.sessionChange', function(response) {
  if (response.session) {
    // A user has logged in, and a new cookie has been saved
    window.location = "/login";
  } else {
    // The user has logged out, and the cookie has been cleared
    window.location = "/logout";
  }
});

So it seems to me like this event is continually firing. Although, I can't test this theory because I can't recreate this scenario locally.

I don't think it's the rails controller. The code here is just:

def login
  # if first time user create db entry

  # now redirect back to where the user came from
  begin
    redirect_to :back
  rescue ActionController::RedirectBackError
    redirect_to root_url
  end
end

Does anyone have any idea on what's going on?

+1  A: 

Sorry I don't have a answer for you, but I was curious if you would be willing to share some of the code involved? I don't want to use Facebooker, and OAuth is ok... but I'd rather use the Javascript SDK as well... But my js skills are weak! I'd greatly appreciate it! Might also help to understand why he is looping...

Also... Cookies disabled perhaps??

Omarvelous
Hi, I'm happy to share the rails code (http://pastebin.com/MEJNtRWH). The control flow that I think is happening: user hits the FB login button; authenticates then comes back to myapp.com which notices the Facebook cookie and uses that to extract @fbhash. We have an event listener on the sessionChange which redirects the browser to myapp.com/login which creates a new user model (if necessary) and redirects the browser back to the original page. There's a little bit of library code that I didn't include in the pastebin which tries to login a user using a session object or the @fbhash.
nafe
@nafe I am trying to use your code but I am failing on the step where you say: "Make sure this gets loaded at init time (in config/initializers)". How is this done exactly?
Jonathan Clark
@Jonathan After you've made the yml file create a new file "load_facebook.rb" in config/initializers/. It'll just be a oneliner like FB_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/facebook.yml")[RAILS_ENV]
nafe