简体   繁体   中英

facebook connect: check if user has permissions with javascript

i am creating login code for facebook connect on our site, however i cannot find how to check if the user has the required permissions.

with the old javascript, one dialog would be opened for each permission and the return code would say if the permission was accepted or not, how does it work with the javascript code?

here is the code i got so far, with the TODO where i want to check if the user has got permissions

   <div id="fb-root"></div>
   <script>
   window.fbAsyncInit = function() {
    FB.init({appId: 'MY API KEY', status: true, cookie: true,xfbml: true});
    FB.Event.subscribe('auth.login', function(response) {
        alert("logged in");

        //TODO: check if all perms has been accepted!!!!
        //if they have NOT been accepted, I want to logout the user
    });

    FB.getLoginStatus(function(response) {

         if (response.session) {    
            // logged in and connected user, again, check if all perms has been accepted
            alert("already logged in");    
         }

     });


  };
  (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>
<fb:login-button perms="email,user_birthday,status_update,publish_stream" >Login with Facebook</fb:login-button>

btw, in the documentation they have this example http://developers.facebook.com/docs/reference/javascript/FB.login

where they use custom buttons, that's why i suspected that there would be something similar for the fb:loginbutton

FB.Event.subscribe('auth.login',function(response) {
   if(response.session) { //checks if session is true
      alert('logged in');

      if(response.perms) { //checks if perms is true = permissions are granted
         alert('perms granted');
      }
      else { //if perms is false = no permissions granted
         alert('no perms');
      }
   }
   else { //if something goes wrong
      alert('login failure');
   }
});

Original Facebook guide: http://developers.facebook.com/docs/reference/javascript/FB.login

I made this solution to check for "user_friends" and "publish_actions" permissions, if not allowed the both, force the user to "re-autenticate". The callback function will only be called when all permissions are given.

function login(cb,forceAuth) {
    FB.getLoginStatus(function (response) {
        if (response.status !== 'connected' || forceAuth==true){
            FB.login(function (response) {
                checkPermissions(cb,false);
            }, {scope: 'publish_actions,user_friends'});
        } else {
            checkPermissions(cb);
        }
    });
}

function checkPermissions(cb,forceAuth){
    FB.api(
        "/me/permissions",
        function (response) {
            if (response.data[0]['publish_actions']==undefined || response.data[0]['publish_actions']==0 ||
                response.data[0]['user_friends']==undefined || response.data[0]['user_friends']==0) {
                if (forceAuth!=false)
                    login(cb,true);
            } else {
                cb();
            }
        }
    );
}

How to use:

login(function () {
    myLogedAndAllowedFunction();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM