Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save brianberlin/443c3bd005ff63282394 to your computer and use it in GitHub Desktop.
Save brianberlin/443c3bd005ff63282394 to your computer and use it in GitHub Desktop.
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
Copy link

Line 47 should be

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

@ikazoy
Copy link

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
Copy link

balassy commented Oct 8, 2017

Thank you, works perfectly!

@RaphiStein
Copy link

Does this create a new user pool user?

@saurabhPV
Copy link

@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
Copy link

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