Skip to content

Instantly share code, notes, and snippets.

@SumindaD
Last active October 1, 2020 14:35
Show Gist options
  • Save SumindaD/230ae7696c63de419c389737808a5f4d to your computer and use it in GitHub Desktop.
Save SumindaD/230ae7696c63de419c389737808a5f4d to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="google-signin-client_id" content="<CLIENT ID>">
<title>Google Auth To AWS</title>
</head>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://apis.google.com/js/platform.js" async defer></script>
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.487.0.min.js"></script>
<div class="g-signin2" data-onsuccess="onSignIn"></div>
<a href="#" onclick="signOut();">Sign out</a>
<p id="log"></p>
<script>
var awsBucketName = '<AWS BUCKET NAME>';
var identityPoolId = '<Identity Pool ID>';
var region = '<Region>';
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
logMessage('Logged in.');
logMessage('Name: ' + profile.getName());
logMessage('Email: ' + profile.getEmail());
var id_token = googleUser.getAuthResponse().id_token;
getCognitoIdentityCredentials(id_token);
}
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
logMessage('User signed out.');
logMessage('');
});
}
function getCognitoIdentityCredentials(id_token){
if(AWS.config.credentials != null)
AWS.config.credentials.clearCachedId();
// Add the Google access token to the Cognito credentials login map.
AWS.config.region = region;
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: identityPoolId,
Logins: {
'accounts.google.com': id_token
}
});
getAWSS3BucketObjects();
}
function getAWSS3BucketObjects(){
var s3 = new AWS.S3();
var params = {
Bucket: awsBucketName
};
s3.listObjects(params, function(err, data) {
if (err) console.log(err, err.stack);
else{
logMessage('');
logMessage('====== S3 Bucket Objects ======');
data.Contents.forEach(element => {
logMessage(element.Key);
});
logMessage('');
}
});
}
function logMessage(message){
$('#log').append(message + '</br>');
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment