Skip to content Skip to sidebar Skip to footer

Why Do Else Cases In Fbasyncinit/fb.event.subscribe Not Work?

I'm wondering why it seems as though the 'else if' case of response.status === 'not authorized' and the 'else' case ('unknown', ie user is not logged into facebook) are not being

Solution 1:

As this question explains, auth.authResponseChange and auth.statuschange don't fire when the user is not logged in. When a user loads the page, the user starts off in the "unknown" state; but once FB.init is called with status:true, Facebook checks the login status and determines that the user is not logged in - which is also called the "unknown" state! Technically, there's no change of state, so the state-change callbacks are not called. (This seems like a major API design flaw, especially since their own sample code doesn't work.)

One solution, which is the accepted answer to that question, is to set the status check in FB.init to false, and instead manually check the status using FB.getLoginStatus() so that we can respond to it ourselves. If the user is not logged in, then subscribe to the login event. If the user is already logged in, that's great.

Here's the code I'm using. login() and logout() are functions I've defined that render parts of the page for the logged-in and not logged-in states. logout() displays a login button, while login() displays user-specific information.

FB.init({
    appId: '...',
    channelUrl: window.location.protocol + '//' + window.location.host + '/channel.html',
    status: false,
    [... other settings ...]
});

FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
        login();
    } else {
        FB.Event.subscribe('auth.login', function(response) {
            window.location.reload();
            login();
        });
        logout();
    }
});

Notice the window.location.reload(); in the case where the user was not logged in originally and then logged themselves in. This is to avoid a similar Blocked a frame... error to what you were experiencing, in my case an http:// vs https:// mismatch. See if you need it in your case. If you're still experiencing a Blocked a frame... error, it may be due to the way you've configured your app settings in Facebook. Try putting http://localhost:8080/ as the site URL under "Website with Facebook Login", and access that site to view your app.

Post a Comment for "Why Do Else Cases In Fbasyncinit/fb.event.subscribe Not Work?"