auth.statusChange doesn't fire during FB.init if the user isn't logged in to facebook

后端 未结 3 776
天命终不由人
天命终不由人 2021-02-05 11:14

I wondered if anyone has found a workaround to the behaviour that I\'m experiencing.

Take the example of the below code:



        
3条回答
  •  天命终不由人
    2021-02-05 11:39

    You can check the response you get back after you pass a true parameter in the getLoginStatus:

    window.fbAsyncInit = function() {
        FB.init({
            appId  : '',
            status : true,
            cookie : true,
            xfbml  : true,
        });
    
        FB.getLoginStatus( function(response) {
            //console.log(response);
            if (response.status === 'connected') {
                var accessToken = response.authResponse.accessToken;
                alert(accessToken);
            } else if (response.status === 'not_authorized') {
                //login function
            } else {
                //login function
            }
        }, true);
    
        FB.Event.subscribe('auth.authResponseChange', function(response) {
            //console.log('The status of the session changed to: '+response.status);
            window.location.reload();
        });
    };
    

    If you set the status to true, the FB.getLoginStatus response object will be cached by the SDK and subsequent calls to FB.getLoginStatus will return data from this cached response.

    To get around this, you have to call FB.getLoginStatus with the second parameter set to true to force a roundtrip to Facebook - effectively refreshing the cache of the response object.

    FB Docs: https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/

提交回复
热议问题