how to detect log out from fb using JS SDK without refreshing the page

穿精又带淫゛_ 提交于 2020-01-06 14:21:20

问题


Hi friends,

I'm developing a website in which Facebook Javascript API is used to send message to our friends.

When the user is logged out of fb,then it shows an error when popping up js API dialog box which is

"Refused to display 'https://www.facebook.com/login.php?api_key=0&skip_api_login=1&display=dialo…cale%3Den_US%26name%3DAnything%26to%3D1791715188%26from_login%3D1&rcount=1' in a frame because it set 'X-Frame-Options' to 'DENY'."

Currenty,I'm using FB.getLoginStatus to detect whether the user is logged out or not. But this will work for only first time,I can't dynamically check it(That means when I logged out of fb account and coming back to the same window and trying to send next message without refresh,it shows the above error.).

Here How Can I dynamically detect that the user is currently logged out. Is there any way to checking FB.getLoginStatus dynamically without page refresh.

I'm showing my code below.

$(document).ready(function(){


window.fbAsyncInit = function() {
 FB.init({appId: '**************', xfbml: true, cookie: true,status:true,oauth:true});
  /*FB.Event.subscribe('auth.login', function() {
          //window.location.reload();
        });*/
     FB.Event.subscribe('auth.logout', function(response) {
    alert('logged out!');
    });
        };

function facebook_send_message(to,name) {  //function called when a button is clicked

 FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
        console.log('how to check');
         var uid = response.authResponse.userID;
        var accessToken = response.authResponse.accessToken;
        send_message(to,name);
    }
    else if (response.status === 'not_authorized') {
            console.log('not_authorized');
        // the user is logged in to Facebook, 
        // but has not authenticated your app
    }
    else
    {
        console.log('hi');
         FB.login(function(response) {
           if (response.authResponse) 
           {
                send_message(to,name);
                //getUserInfo(); // Get User Information.

            } else
            {
             console.log('Authorization failed.');
            }
         },{scope: 'email'});
    }
});
}

send_message(to,name); will send message if user is logged in, Currently for the first time,it works clearly,but from the second time without page refresh and if we logged out from fb, then FB.getLoginStatus still shows response.status as 'connected' and hence error occured.

Waiting for your kind replies. Thanks in Advance


回答1:


According to Facebook,

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

To improve the performance of your application, not every call to check the status of the user will result in request to Facebook's servers. Where possible, the response is cached. The first time in the current browser session that FB.getLoginStatus is called, or the JS SDK is init'd with status: true, the response object will be cached by the SDK. Subsequent calls to FB.getLoginStatus will return data from this cached response.

This can cause problems where the user has logged into (or out of) Facebook since the last full session lookup, or if the user has removed your application in their account settings.

To get around this, you 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.getLoginStatus(function(response) {
  // this will be called when the roundtrip to Facebook has completed
}, true);


来源:https://stackoverflow.com/questions/24968034/how-to-detect-log-out-from-fb-using-js-sdk-without-refreshing-the-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!