Facebook API - Detect if session is active OR NOT active

Posted by Tom Doe on Stack Overflow See other posts from Stack Overflow or by Tom Doe
Published on 2012-06-28T00:29:42Z Indexed on 2012/06/28 3:16 UTC
Read the original article Hit count: 119

I'm sure I'm simply overlooking something in the Facebook API docs. Basically, after I've loaded the FB Graph, I need to know if the session is active... I cannot simply assume they're logged out and simply re-render if they're logged in once the 'auth.statusChange' event is triggered. I need to know right off the bat.

Below is the code that I've used. Most importantly the FB.getLoginStatus/getAuthResponse/getAccessToken don't work like I'd expect; essentially where it indicates, when invoked, whether they're logged in or out.

(function(d) {

    // Create fb-root
    var fb_root = document.createElement('div');
        fb_root.id = "fb-root";
        document.getElementsByTagName('body')[0].appendChild( fb_root );

    // Load FB Async
    var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement('script'); js.id = id; js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js";
    ref.parentNode.insertBefore(js, ref);

    // App config-data
    var config = {
        appId : XXXX,
        cookie: true,
        status: true,
        frictionlessRequests: true
    };

    window.fbAsyncInit = function() {
        // This won't work.
        // I can't assume they're logged out and rely on this to tell me they're logged in.
        FB.Event.subscribe('auth.statusChange', function(response) {});

        // Init
        FB.init(config);

        // These do not inidicate if the user is logged out :(
        FB.getLoginStatus(function(response) { });
        FB.getAuthResponse(function(response) { });
        FB.getAccessToken(function(response) { });
    };

}(document));

Any help is much appreciated. :)

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about facebook