Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AWS Cognito + Facebook Login JavaScript Example
<!DOCTYPE html>
<html>
<head>
<title>AWS Cognito + Facebook Login JavaScript Example</title>
<meta charset="UTF-8">
</head>
<body>
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.1.41.min.js"></script>
<script>
AWS.config.region = 'us-east-1';
(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'));
window.fbAsyncInit = function() {
FB.init({
appId : 'XXXXXXX',
cookie : true,
xfbml : true,
version : 'v2.2'
});
FB.getLoginStatus(statusChangeCallback);
};
function statusChangeCallback(response) {
console.log('statusChangeCallback', response);
if (response.status === 'connected' && response.authResponse) {
testAPI();
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'us-east-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
Logins: { 'graph.facebook.com': response.authResponse.accessToken }
});
AWS.config.credentials.get(function(err) {
if (err) return console.log("Error", err);
console.log("Cognito Identity Id", AWS.config.credentials.identityId);
});
} 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 testAPI() {
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>
<fb:login-button scope="public_profile,email" onlogin="FB.getLoginStatus(statusChangeCallback);"></fb:login-button>
<div id="status"></div>
</body>
</html>
@masayang

This comment has been minimized.

Copy link

@masayang masayang commented Aug 13, 2017

Line 47 should be

console.log("Cognito Identity Id", AWS.config.credentials.data.identityId);

@ikazoy

This comment has been minimized.

Copy link

@ikazoy ikazoy commented Aug 31, 2017

Thank you for putting great example.

@masayang
For me, AWS.config.credentials.identityId worked though AWS.config.credentials.data.identityId is undefined.

@balassy

This comment has been minimized.

Copy link

@balassy balassy commented Oct 8, 2017

Thank you, works perfectly!

@RaphiStein

This comment has been minimized.

Copy link

@RaphiStein RaphiStein commented Jan 8, 2018

Does this create a new user pool user?

@saurabhPV

This comment has been minimized.

Copy link

@saurabhPV saurabhPV commented Jan 12, 2018

@RaphiStein No it doesn't. You will find this user in Federated Identity section on Cognito.
The only way to create FB user in cognito is by redirecting it to

https://your_domain.auth.us-east-1.amazoncognito.com/oauth2/authorize?redirect_uri=your_redirect_uri&response_type=token&client_id=xxxx& identity_provider=Facebook

Have a look at the @douglasgimli 's comment on amazon-archives/amazon-cognito-identity-js#608

@albogdano

This comment has been minimized.

Copy link

@albogdano albogdano commented Apr 16, 2020

Some extra information can be found here: https://www.integralist.co.uk/posts/cognito/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment