views:

1299

answers:

1

I'm trying to do what is supposed to be fairly simple with facebook connect, but having no luck.

When a user logs in, I want to show that users details (refreshed without reloading the page), and when the user logs out, I need to go back to a logged in state.

The code I have is

   function update_user_box(){
     jQuery('span#loggedin').html('');
    jQuery('span#fbLogin, ul#otherLogin').hide();
    jQuery('div#popForecast li.otherLogin, ul.showList li.otherLogin').remove();
    jQuery('span#logged').text('1');
    FB.XFBML.Host.parseDomTree();
}

function fbLogout(){
   FB.Connect.logout();
   jQuery('span#fbLogin').show();
   jQuery('span#loggedin').empty();
   jQuery('span#logged').text('0');
}
        FB.init('c0529b8c11709f2317ae643d854e3866', 'xd_receiver.htm');
     FB.ensureInit(function(){
   FB.Connect.ifUserConnected(update_user_box);
   });

I can log the user in and out, but nothing else changes. I can't even trigger an alert from my functions.

For some reason the html I'm using won't display within the pre tags, however, on reloading the page, the user is displayed (or not as it should), so it is just not being triggered directly from the javascript functions. But refreshing the page launches the correct function.

The error I get in firebug is 'fbLogout undefined', but clearly it is defined. Does it matter where I define it? Currently all of the javascript/jquery is in the head within script tags. Though I've tried moving it down into the page and didn't have luck their either.

+3  A: 

Turns out I was over thinking this. FB does a lot of the work for you. I was calling the functions on the login and logout events, but with FB.Connect.isUserConnected function, FB keeps an eye on this for you.

My code is exactly the same except 1) i no longer have onclick events in the fb:login button 2) the FB.Connect.isUserConnected function now looks like this

FB.Connect.isUserConnected(update_user_box, fbLogout);
pedalpete
+1 for for taking the time to post your own answer.
zombat
I second. Having an answer helps.
Ryszard Szopa