2

I'm trying to upgrade to the 3.1 SDK and running into some problematic behavior: When I log in via the FB login button on my site, the page reloads, despite the fact that I have my own JS method defined in the login button's "onlogin" event.

Because the page instantly reloads, it appears that my own "onlogin" script (which checks the FB user and logs them into MY site) seems to get cut off or does not fire. Thus, the user cannot log in to the site.

The other side effect is peculiar: After logging into FB vi my site's FB button, I will show up as being logged in at Facebook.com, but after a few seconds, a popup will appear at Facebook.com saying "Not logged in. Please log in to continue (YES NO)."

As usual I can't find help in the docs and am getting really stumped. Here is my FB init:

<script>
window.fbAsyncInit = function() {
    FB.init({
        appId: '<?php echo cConfiguration::$facebookAppId;?>',
        cookie: true,
        xfbml: true,
        oauth: true
    });
    FB.Event.subscribe('auth.logout', function(response) {
        window.location = '/signout.php5';
    });
};
(function() {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol +
        '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>   

The login button is:

<fb:login-button length="long" autologoutlink="false" onlogin="fbLogin();"></fb:login-button>

EDIT I've also tried this button code without any of the options or the "onlogin" event and I get the same behavior.

So my questions:

  • Is it expected that the page should reload after one logs in via the login button, even though have not subscribed the auth.login event in my FB.init?
  • If so, is there a way to override that behavior?
  • Is something else wrong here?

3 Answers 3

1

You have attached an event handler for logout but not for login. Add this.

FB.Event.subscribe('auth.login', function(response) {
return false;
});

Although, preventing the reload is probably not what you want to do. Instead what I have found works well is to detect if the user has clicked the login button or not. THe process is.

  1. On page load (well actually after FB.init) facebook will automatically login.
  2. You should handle this login and log the user into your site via an ajax call. After this login is successful set a JS variable that states the user is logged in.
  3. Attach an event to the login button. Once the user 'clicks' the login button check to see if they are logged in and refresh the page.
3
  • I was so excited to see your idea I had to try it right away. Unfortunately I still get the reload to happen whenever a Facebook login is detected. I even tried adding onlogin="return false" in the facebook button. I verified that event handler was still firing by adding a debug alert. As to your second point: I'm almost coming to terms with the idea that Facebook wants sites to work as you describe. However, if I can just prevent this auto refresh from happening, I won't have to rewrite any code, which had been working since May.
    – sersun
    Aug 26, 2011 at 3:36
  • Disregard my statement about "verifying that the event handler fired" by adding a debug alert. Perhaps the reload is caused by something else entirely. The other thing that puzzles me is how the login isn't "permanent". After logging in via the login button, I can go to facebook and refresh my personal page, but after a few seconds a popup appears telling me I have to log in. At that point my Facebook session is destroyed and I have to log in again. I'm wondering if these issues are related.
    – sersun
    Aug 26, 2011 at 3:49
  • It sounds like something is calling the facebook logout either on your site FB.logout() or on another site. The facebook 'best practice' is that when you click logout on a site it should log you out of ALL facebook sessions.
    – Ryan White
    Aug 26, 2011 at 14:56
1

Ok, I found out the cause of seemingly uncontrollable page reloads

For some reason the auth.logout event was being triggered whenever I would log IN via the Facebook login prompt.

So I changed this line:

FB.Event.subscribe('auth.logout', function(response) {
    window.location = '/signout.php5';
});

to

FB.Event.subscribe('auth.logout', function(response) {
    return false;
});
1
  • If you still have questions, just open a new question and cite this post / answer for context.
    – user50049
    Aug 31, 2011 at 3:03
0

Use this as your Login Button, i believe there must be something in your fbLogin(); param that is reloading the page. Normal Behaviour is to just refresh the button, page refresh has to be forced with Event.subscribe.

<fb:login-button autologoutlink="false"></fb:login-button>
1
  • I tried that and I still get the page reload. I also tried earlier with no other options in the fb login button code. That is the mystery to me. If I'm not subscribing the auth.login event to reload the page and I have no "onlogin" event, why is there a reload happening at all?
    – sersun
    Aug 25, 2011 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.