Skip to content

Instantly share code, notes, and snippets.

@evandiewald
Created July 28, 2021 19:36
Show Gist options
  • Save evandiewald/f46f45f1874d6124a345994e2ddacae8 to your computer and use it in GitHub Desktop.
Save evandiewald/f46f45f1874d6124a345994e2ddacae8 to your computer and use it in GitHub Desktop.
Javascript from index.html
// javascript snippet from index.html
<script src="https://apis.google.com/js/platform.js" async defer></script>
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.953.0.min.js">
</script><meta name="google-signin-client_id" content="{GOOGLE_CLIENT_ID}">
<script>
var AWS = require("aws-sdk");
AWS.config.region = 'us-east-1';
function signinCallback(googleUser) {
var profile = googleUser.getBasicProfile();
console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
console.log('Name: ' + profile.getName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail());
document.getElementById('profile-email').innerHTML = profile.getEmail();
document.getElementById('profile-name').innerHTML = profile.getName();
document.getElementById('profile-image').setAttribute('src', profile.getImageUrl());
document.getElementById('profile-card').hidden = false;
document.getElementById('signin-button').hidden = true;
document.querySelector('.fruit-list').hidden = false;
AWS.config.credentials = new AWS.WebIdentityCredentials({
RoleArn: '{ROLE_ARN}',
ProviderId: null, // this is null for Google
WebIdentityToken: googleUser.getAuthResponse().id_token
});
// Obtain AWS credentials
AWS.config.credentials.get(async function(){
// Access AWS resources here.
var accessKeyId = AWS.config.credentials.accessKeyId;
var secretAccessKey = AWS.config.credentials.secretAccessKey;
var sessionToken = AWS.config.credentials.sessionToken;
const response = await fetch('http://localhost:8000/fruits', {
method: 'POST',
body: JSON.stringify({
'AccessKeyId': accessKeyId,
'SecretAccessKey': secretAccessKey,
'SessionToken': sessionToken
}),
headers: {
'Content-Type': 'application/json'
}
});
const myJson = await response.json(); //extract JSON from the http response
const fruits = JSON.parse(myJson['fruits']);
console.log(typeof fruits);
console.log(fruits);
var str = ''
var arrayLength = fruits.length;
for (var i = 0; i < arrayLength; i++) {
str += '<li class="list-group-item d-flex justify-content-between align-items-center">' + fruits[i]['name'] +
'<button type="button" class="btn btn-primary position-relative">$' + fruits[i]['price'] +
'<span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger">\n' +
fruits[i]['quantity'] +
'</span>' +
'</button></li>'
// str += '<li class="list-group-item d-flex justify-content-between align-items-center">' + fruits[i]['name'] + ' <b>Price: $' + fruits[i]['price'] + '</b> ' + '<span class="badge bg-primary rounded-pill">' + fruits[i]['quantity'] + '</span></li>'
}
document.getElementById('fruit-list').innerHTML = str;
});
}
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('User signed out.');
document.getElementById('profile-card').hidden = true;
document.getElementById('fruit-list').innerHTML = null;
document.getElementById('signin-button').hidden = false;
});
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment