Check if user is logged in New Facebook API

后端 未结 1 1403
天涯浪人
天涯浪人 2020-12-02 17:40

well I\'m new to the facebook api stuff, and just recently someone told me I was using the old api.

So now, I can\'t seem to figure out how to check if the user is l

相关标签:
1条回答
  • 2020-12-02 18:24

    Here is an example where fbLoginStatus() function will be automatically called when user logs in/logs out, so you can decide which div to display there.

    <body>
    <div id="fb-root"></div>
    <script>
      function fbLoginStatus(response) {
         if( response.status === 'connected' ) {
            //user is logged in, display profile div
         } else {
            //user is not logged in, display guest div
         }
      }
      window.fbAsyncInit = function() {
        FB.init({appId: 'your app id', status: true, cookie: true, xfbml: true});
        FB.getLoginStatus(fbLoginStatus);
        FB.Event.subscribe('auth.statusChange', fbLoginStatus);
      };
      (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol +
          '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
      }());
    </script>
    

    Now if you need to check if user is logged in right now (for example on button click):

    if(FB.getSession() != null) {
       //logged user id: FB.getSession().uid
    } else {
      //user is not logged in
    }
    

    Sorry, don't know any good tutorials.

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