I am using the following code for Facebook login api :-
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
if (response.status === 'connected') {
testAPI();
} else if (response.status === 'not_authorized') {
document.getElementById('status').innerHTML = 'Please log ' +
'into this app.';
} else {
document.getElementById('status').innerHTML = 'Please log ' +
'into Facebook.';
}
};
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
};
window.fbAsyncInit = function() {
FB.init({
appId : '924067597708287',
cookie : true,
xfbml : true,
version : 'v2.5'
});
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
};
(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'));
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
console.log('Successful login for: ' + response.id);
console.log('Successful login for: ' + response.picture);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + '!';
document.getElementById('pic').innerHTML =
'Your user id is : ' + response.id + "<br />" +
"<img src='" + "https://graph.facebook.com/" + response.id + "/picture?type=large' alt='' />";
});
};
When I place the same code, within angular controller, I get an error message
checkLoginState is not defined
The login process stalls, but when I refresh, it shows logged in.
Please help.
Once you put checkLoginState
into a controller, is it out of scope from FB (the sdk.js). So, define it on the window instead (you will need to inject $window
).
$window.checkLoginState = function() {
FB.getLoginStatus(function(response) {
console.log("checkLoginState: "+response);
});
}
You should then see this in the console after every click, which tells you that FB can reach the function:
checkLoginState: [object Object]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With