The Basics

jQuery Plugin to simplify the process of requesting Facebook permissions, such that you only proceed forward in sign-up once the user has granted all you require.

Installation

Basically just include the file and set up the Facebook API on the page.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="/libs/jquery-facebook-authorize/jquery.facebook-authorize.min.js"></script>

<script>
window.fbAsyncInit = function() {
    FB.init({
        appId   : XXXXXXXXXXXXXXXXXXXXXXX,
        cookie  : true,
        xfbml   : true, /* parse social plugins on this page. */
        version : 'v2.5'
    });
};

(function(d, s, id){
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>

<div id='error_msgs'></div>

<div class='row'>
  <button id='facebook_request' class="btn btn-default" style="color:#0b62a4;">
    <i class="fa fa-facebook-square"></i> Login with Facebook
  </button>
</div>

<script>
    $(function() {
        var $fbreq = $('#facebook_request');

        var options = {required : ['public_profile', 'email', 'user_friends', 'publish_actions'],
                       optional: []};
        $fbreq.facebookAuthorize(options);

        $fbreq.on('facebook-request-clicked', function(event) {
            $('#error_msgs').empty();
        });
        $fbreq.on('facebook-missing-required', function(event, missing) {
            $('#error_msgs').text('missing: ' + JSON.stringify(missing));
        });
        $fbreq.on('facebook-success', function(event, granted, missing, details) {
            /* move the user onto whatever step applies. */
            createOrLogin(granted, details);
        });
        $fbreq.on('facebook-not-authorized', function(event) {
            $('#error_msgs').text('not authorized');
        });
        $fbreq.on('facebook-not-loggedin', function(event) {
            $('#error_msgs').text('not logged in...');
        });
    });
</script>

Usage

Name Type Default Description
required array [] List of required permissions.
optional array [] List of optional permissions.

Events

Event Handler
facebook-request-clicked function(event):
- event - jQuery event
facebook-missing-required function(event, missing):
- event - jQuery event
- missing - comma separated string of missing required permissions
facebook-success function(event, granted, missing, details):
- event - jQuery event
- granted - comma separated string of all permissions granted
- missing - comma separated list of any missing optional permissions
- details - Facebook API response.authResponse object
facebook-not-authorized function(event):
- event - jQuery event
facebook-not-loggedin function(event):
- event - jQuery event

License

Apache License 2.0