How to detect user logging out of Facebook after logging into my app?

前端 未结 2 737
逝去的感伤
逝去的感伤 2021-01-20 06:24

My application uses Facebook authentication:

FB.init({

    appId: config.fbAppId,
    status: true,
    cookie: true,
//  xfbml: true,
//  channelURL : \'ht         


        
相关标签:
2条回答
  • 2021-01-20 06:51

    What you could try and use is the FB.getLoginStatus where if the user is connected this would allow them to complete the wall post. If they aren't connected then call the FB.login method before they can post on the wall.

    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
            // the user is logged in and has authenticated your
            // app, and response.authResponse supplies
            // the user's ID, a valid access token, a signed
            // request, and the time the access token 
            // and signed request each expire
            var uid = response.authResponse.userID;
            var accessToken = response.authResponse.accessToken;
        } else if (response.status === 'not_authorized') {
            // the user is logged in to Facebook, 
            // but has not authenticated your app
        } else {
            // the user isn't logged in to Facebook.
        }
    });
    

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

    There are also the events for login and logout that you can watch for and do something with those responses.

    FB.Event.subscribe('auth.login', function(response) {
        // do something with response
    });
    
    FB.Event.subscribe('auth.logout', function(response) {
        // do something with response
    });
    

    http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/

    0 讨论(0)
  • 2021-01-20 06:58

    Just recall getLoginStatus BUT forcing a roundtrip to Facebook. Look following code:

    FB.getLoginStatus(function(response) {
      // some code
    }, true);
    

    Look the last parameter set to true to force the roundtrip.

    From JS SDK documentation:

    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. (http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/)

    0 讨论(0)
提交回复
热议问题