Skip to content

Instantly share code, notes, and snippets.

@livercake
Last active August 29, 2015 14:06
Show Gist options
  • Save livercake/409555b49bcff3f494f6 to your computer and use it in GitHub Desktop.
Save livercake/409555b49bcff3f494f6 to your computer and use it in GitHub Desktop.
Very simple FB Login w/javascript+fb sdk
// This is called with the results from from FB.getLoginStatus().
// Welcome: I am the Gatekeeper. Are you the Keymaster?
function theGatekeeper(response) {
console.log('FB Login object response:');
console.log(response);
// FB.getLoginStatus() responds
if (response.status === 'connected') {
// logged into FB + permissions OK: You are the Keymaster
console.log('login OK, running ');
theKeymaster();
} else if (response.status === 'not_authorized') {
// logged into FB + permissions FAIL: You are NOT the Keymaster
document.getElementById('status').innerHTML = 'Click to grant permissions ' +
'to App.';
} else {
// not logged into FB + permissions UNKNOWN: You are NOT the Keymaster and you DO NOT have the key
document.getElementById('status').innerHTML = 'Click to login ' +
'in Facebook.';
}
}
// Check login status. Run the Gatekeeper in response to login button.
function checkLoginState() {
FB.getLoginStatus(function(response) {
theGatekeeper(response);
});
}
// Init FB SDK on load
window.fbAsyncInit = function() {
FB.init({
appId : '{{app-id}}',
cookie : true, // enable cookies
xfbml : true, // parse social plugins on this page
version : 'v2.1' // use version 2.1
});
// Providing callbacks for the Gatekeeper via checkLoginState
FB.getLoginStatus(function(response) {
theGatekeeper(response);
});
};
// Load the SDK asynchronously
(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'));
// Prying into FB.api: I am the Keymaster
function theKeymaster() {
console.log('FB Login OK');
FB.api('/me', function(response) {
console.log('Graph API OK: Welcome, ' + response.name);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment