Skip to content

Instantly share code, notes, and snippets.

@tsuginodan
Created September 5, 2016 23:30
Show Gist options
  • Save tsuginodan/2961c681787421396c128da5a3c8f68a to your computer and use it in GitHub Desktop.
Save tsuginodan/2961c681787421396c128da5a3c8f68a to your computer and use it in GitHub Desktop.
FB re-request permissions. Not my solution, I found it on SO.
<!doctype>
<html>
<head>
<title></title>
</head>
<body>
<fb:login-button data-scope="public_profile,email" onlogin="checkLoginState();" data-size="large"></fb:login-button>
<div id="status"></div>
<br/>
<fb:like data-share="true" data-width="450" data-show-faces="true"></fb:like>
<script>
/*CALLED AFTER FB LOGIN MODAL*/
function checkLoginState() {
/*CHECK FOR DENIED PERMISSIONS*/
FB.api('/me/permissions', function(response) {
var declined = [];
for (var i = response.data.length - 1; i >= 0; i--) {
if(response.data[i].status == 'declined'){declined.push(response.data[i].permission);}
};
if(declined.length)
{
var error_text = ERROR_MESSAGE;
console.info("EMAIL PERMISSION IS REQUIRED.");
document.getElementById('status').innerHTML = error_text;
/*YOU SHOULD LOGOUT SO YOU CAN RE-REQUEST PERMISSIONS*/
FB.logout(function (response) {
console.info("LOGOUT.");
})
/*NOTIFY USER YOU REQUEST SPECIFIC PERMISSION*/
if(confirm(error_text))
openLoginModal();
}
else
{
/*CONTINUE WITH LOGIN*/
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
});
}
/*OPEN LOGIN MODAL. 'RE-REQUEST PERMISSIONS'*/
function openLoginModal() {
FB.login(function (response) {
if(response.authResponse)
checkLoginState();
}, {scope: 'email', auth_type: 'rerequest'});
}
/*SAME AS FB DOCS*/
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
if (response.status === 'connected') {
requestFBInfo();
} 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.';
}
}
/* Here we run a very simple test of the Graph API after login is
successful. See statusChangeCallback() for when this call is made.*/
/*SINCE GRAPH API V2.4 , YOU NEED TO SPECIFY FIELDS*/
function requestFBInfo() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', {fields: 'id,name,email'}, function(response) {
console.log(response);
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + '!. with email ' + response.email;
});
}
var FBAppId = YOUR_APP_ID;
/*INICIAR FB SDK*/
window.fbAsyncInit = function() {
FB.init({
appId : FBAppId,
cookie : true,
status : true,
xfbml : true,
version : 'v2.7'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];/*TOMARA EL PRIMER TAG EN BODY*/
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/es_LA/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment