Skip to content

Instantly share code, notes, and snippets.

@TastyToast
Last active December 15, 2015 03:19
Show Gist options
  • Save TastyToast/5193251 to your computer and use it in GitHub Desktop.
Save TastyToast/5193251 to your computer and use it in GitHub Desktop.
Login and Get Perms with a click of a button
// LOGIN AND GET PERMS WHEN THE USER CLICKS <div id="yes"><a></a></div>
(function($){
var app = app || {
init: function(){
console.log("INIT");
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
$(document).on('click', '#yes a', function(){
app.getData();
});
} else if (response.status === 'not_authorized') {
// Logged in but not authorized
$(document).on('click', '#yes a', function(){
app.login(app.getData);
});
} else {
// the user isn't logged in to Facebook.
$(document).on('click', '#yes a', function(){
app.login(app.getData);
});
}
});
},
login: function(callback){
FB.login(function(response) {
if (response.authResponse) {
if(typeof callback == 'function'){
callback();
} else {
$(document).on('click', '#yes a', function(){
app.getData();
});
}
} else {
alert('You must accept permissions to proceed');
}
}, {scope: 'email'});
},
getData: function(){
FB.api('/me', function(response){
// Response contains all the user FB data
console.log(response);
})
}
};
$(document).on('afterfbinit', function(){
app.init();
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment