Skip to content

Instantly share code, notes, and snippets.

@saltukalakus
Created January 7, 2019 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saltukalakus/1161acfeba46eb89b422c321cc9a7946 to your computer and use it in GitHub Desktop.
Save saltukalakus/1161acfeba46eb89b422c321cc9a7946 to your computer and use it in GitHub Desktop.
Test facebook - add your appId in FB.init - check console logs for the user profile
<html>
<head>
<title>Facebook Login JavaScript Example</title>
<meta charset="UTF-8">
<script>
// This is called with the results from from FB.getLoginStatus().
function statusChangeCallback(response) {
// The response object is returned with a status field that lets the app know the current login status of the person.
if (response.status === 'connected') {
console.log('Welcome! Fetching your information.... ');
FB.api('/me?fields=first_name,last_name,email', function(response) {
console.log('Successful login for: ')
console.dir(JSON.stringify(response));
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + '!';
});
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
document.getElementById('status').innerHTML = 'Please log ' + 'into this app.';
} else {
// The person is not logged into Facebook, so we're not sure if they are logged into this app or not.
document.getElementById('status').innerHTML = 'Please log ' + 'into Facebook.';
}
}
window.fbAsyncInit = function() {
FB.init({
appId : 'YOUR_APP_ID',
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse social plugins on this page
version : 'v2.7' // use version 2.1
});
// Now that we've initialized the JavaScript SDK, we call FB.getLoginStatus().
// This function gets the state of the person visiting this page.
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
};
function buttonClick() {
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me?fields=first_name,last_name,email', function(response) {
console.log('Good to see you, ' + response.name + '.');
console.log(JSON.stringify(response));
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, { scope: 'email',
return_scopes: true});
}
// 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'));
</script>
</head>
<body>
<button onclick="buttonClick()">Login</button>
<div id="status">
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment