I managed to develop the login/logout using the facebook js sdk.
After login, I am able to get the email of the user.
I am having trouble when I try to get the information for a authentified user, after I refresh the entire page.
Somehow, the FB object is not there anymore.
I get the error:
Uncaught ReferenceError: FB is not defined
How can I init the FB object ?
ps: I have already read and coded the solution from:
but it does not work.
In that example, /me is replaced with /l214.animaux
I don't know the user profile id.
My code is divided in 2 php files:
index.php:
<div class="fb-login-button" onlogin="checkLoginState();" data-scope="public_profile,email" data-max-rows="1" data-size="xlarge" data-show-faces="false" data-auto-logout-link="true"></div>
and facebook_login.php:
<!DOCTYPE html>
<html lang="en">
<head>
<?php
$title = Yii::app()->name;
?>
<title><?php echo $title; ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
</head>
<body class="inner-page page-produse">
<div id="fb-root"></div>
<script>
(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#xfbml=1&version=v2.8&appId=1813020315643394";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
function api_call_get_info_about_the_logged_in_user() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', {fields: 'email'}, function (response) {
console.log('Successful login for: ' + response.email);
});
}
window.fbAsyncInit = function () {
FB.init({
appId: '1813020315643394',
xfbml: true,
version: 'v2.8'
});
api_call_get_info_about_the_logged_in_user();
};
function checkLoginState() {
FB.getLoginStatus(
function (response) {
console.log('facebook get login status');
console.log(response);
if (response.status === 'connected') {
// Logged into your app and Facebook.
api_call_get_info_about_the_logged_in_user();
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
} else {
// The person is not logged into Facebook, so we're not sure if
// they are logged into this app or not.
}
}
);
}
</script>
<div class="wrapper">
<div class="site-wrapper">
<?php
echo $content;
?>
<div class="site-wrapper-overlay"></div>
</div> <!-- .site-wrapper -->
</div> <!-- .wrapper -->
</body>
</html>