<!doctype html>
<html lang="en">
<head></head>
<body>
<h2>Add Facebook Login to your webpage</h2>
<!-- Set the element id for the JSON response -->
<p id="profile"></p>
<button onclick="loginWithFacebook()">Log in with Facebook</button>
<script>
// Initialize the Facebook JavaScript SDK, but wait till it's loaded
window.fbAsyncInit = function () {
FB.init({
appId: "{your-facebook-app-id}",
xfbml: true,
version: "{the-graph-api-version-for-your-app}",
});
};
// Handle clicks on the "Log in with Facebook" button
function loginWithFacebook() {
FB.login(function (response) {
if (response.authResponse) {
console.log("Welcome! Fetching your information.... ");
// After successful login, we can fetch the user's information
FB.api("/me", { fields: "name, email" }, function (response) {
document.getElementById("profile").innerHTML =
"Good to see you, " +
response.name +
". i see your email address is " +
response.email;
});
} else {
console.log("User cancelled login or did not fully authorize.");
}
});
}
// Load the JavaScript 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 = "https://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
})(document, "script", "facebook-jssdk");
</script>
</body>
</html>
FB.getLoginStatus starts a call to Facebook to get the login status. Facebook then calls your callback function with the results.
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
{
status: 'connected',
authResponse: {
accessToken: '{access-token}',
expiresIn:'{unix-timestamp}',
reauthorize_required_in:'{seconds-until-token-expires}',
signedRequest:'{signed-parameter}',
userID:'{user-id}'
}
}
status specifies the login status of the person using the webpage. The status can be one of the following:Status Type
| Description |
|---|---|
connected | The person is logged into Facebook, and has logged into your webpage. |
not_authorized | The person is logged into Facebook, but has not logged into your webpage. |
unknown | The person is not logged into Facebook, so you don’t know if they have logged into your webpage. Or FB.logout() was called before, and therefore, it cannot connect to Facebook. |
connected, the following authResponse parameters are included in the response:authResponse Parameters
| Value |
|---|---|
accessToken | An access token for the person using the webpage. |
expiresIn | A UNIX time stamp when the token expires. Once the token expires, the person will need to login again. |
reauthorize_required_in | The amount of time before the login expires, in seconds, and the person will need to login again. |
signedRequest | A signed parameter that contains information about the person using your webpage. |
userID | The ID of the person using your webpage. |
FB.login().FB.login(function(response){
// handle the response
});
scope parameter can be passed along with the FB.login() function call. This optional parameter is a list of permissions, separated by commas, that a person must confirm to give your webpage access to their data. Facebook Login requires dvanced public_profile permission, to be used by external users.FB.login(function(response) {
// handle the response
}, {scope: 'public_profile,email'});
authResponse object to the callback specified when you called FB.login(). This response can be detected and handled within the FB.login().FB.login(function(response) {
if (response.status === 'connected') {
// Logged into your webpage and Facebook.
} else {
// The person is not logged into your webpage or we are unable to tell.
}
});
FB.logout() to a button or a link.FB.logout(function(response) {
// Person is now logged out
});
{app-id} with your app ID and {api-version} with the Graph API version to use. Unless you have a specific reason to use an older version, specify the most recent version: v25.0.
<!DOCTYPE html>
<html>
<head>
<title>Facebook Login JavaScript Example</title>
<meta charset="UTF-8">
</head>
<body>
<script>
function statusChangeCallback(response) { // Called with the results from FB.getLoginStatus().
console.log('statusChangeCallback');
console.log(response); // The current login status of the person.
if (response.status === 'connected') { // Logged into your webpage and Facebook.
testAPI();
} else { // Not logged into your webpage or we are unable to tell.
document.getElementById('status').innerHTML = 'Please log ' +
'into this webpage.';
}
}
function checkLoginState() { // Called when a person is finished with the Login Button.
FB.getLoginStatus(function(response) { // See the onlogin handler
statusChangeCallback(response);
});
}
window.fbAsyncInit = function() {
FB.init({
appId : '{app-id}',
cookie : true, // Enable cookies to allow the server to access the session.
xfbml : true, // Parse social plugins on this webpage.
version : '{api-version}' // Use this Graph API version for this call.
});
FB.getLoginStatus(function(response) { // Called after the JS SDK has been initialized.
statusChangeCallback(response); // Returns the login status.
});
};
function testAPI() { // Testing Graph API after login. See statusChangeCallback() for when this call is made.
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + '!';
});
}
</script>
<!-- The JS SDK Login Button -->
<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">
</fb:login-button>
<div id="status">
</div>
<!-- Load the JS SDK asynchronously -->
<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js"></script>
</body>
</html>