Skip to content

Instantly share code, notes, and snippets.

@fraserr
Forked from kimerran/fblogin-handler.js
Created October 2, 2018 23:14
Show Gist options
  • Save fraserr/3e03828b724e33c6aa86ffce16f5e211 to your computer and use it in GitHub Desktop.
Save fraserr/3e03828b724e33c6aa86ffce16f5e211 to your computer and use it in GitHub Desktop.
facebook login test
var FbApp = {
init: {
appId: '144919695582243',
cookie: true,
xfbml: true,
version: 'v2.1'
},
// called when user is currently logged-in and already authorized the app
onConnected: function() {
$("#fblogin").hide();
FB.api('/me', function(response) {
console.log("%o", response);
$('#status').html('Thanks for logging in, ' + response.email + '!');
});
},
// called when user is logged-in but not yet authorized the app
onNotAuthorized: function() {
console.log('app not authorized!');
},
// called when user is not logged-in
onLoggedOut: function() {
console.log('you are logged out!');
}
}
window.fbapp = FbApp;
<!DOCTYPE html>
<html>
<head>
<title>Facebook Login JavaScript Example</title>
<meta charset="UTF-8">
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
</head>
<body>
<script src="fblogin-handlers.js"></script>
<script src="fblogin.js"></script>
<div id="fblogin">
<fb:login-button scope="public_profile,email" onlogin="checkLoginState();"></fb:login-button>
</div>
<div id="status">
</div>
</body>
</html>
var fbapp = window.fbapp;
function statusChangeCallback(response) {
if (response.status === 'connected') {
fbapp.onConnected();
} else if (response.status === 'not_authorized') {
fbapp.onNotAuthorized();
} else {
fbapp.onLoggedOut();
}
}
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
window.fbAsyncInit = function() {
FB.init({
appId : fbapp.init.appId,
cookie : fbapp.init.cookie,
xfbml : fbapp.init.xfbml,
version : fbapp.init.version
});
FB.getLoginStatus(function(response) {
statusChangeCallback(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'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment