(function () { 'use strict'; var Auth = { identityProvider: "COGNITO", identityPoolId: "us-west-2:8e3a0171-3a11-42b1-9705-cf23b9fe07ff", tokenScopes: [ "openid" ], authDomain: "hdrndgl-loginprovider.auth.us-west-2.amazoncognito.com", userPoolWebClientId: "ms6bvd98n99v6a6ggohl9qesm", mandatorySignIn: true, region: "us-west-2", userPoolId: "us-west-2_9dI8B4Ye1" }; var ImageBucket = "imageuploadcfnstack-imguploadbucket-11jhoo5c21r6w"; var config = { Auth: Auth, ImageBucket: ImageBucket }; let auth = undefined; let creds = undefined; function showError() { console.log.apply(arguments); let ErrorPane = document.getElementById('ErrorPane'); ErrorPane.classList.add('error'); ErrorPane.innerText = ""; for(let argument of arguments) { ErrorPane.innerText += err; } return -1; } function upload() { let idp = `cognito-idp.${config.Auth.region}.amazonaws.com/${config.Auth.userPoolId}`; AWS.config.region = config.Auth.region; AWS.config.credentials = new AWS.CognitoIdentityCredentials({ IdentityPoolId: config.Auth.identityPoolId, Logins: { [idp]: creds.idToken.jwtToken } }); AWS.config.credentials.refresh((error) => { if (error) { showError(error); } else { console.log('Successfully logged!'); var s3 = new AWS.S3({ apiVersion: '2006-03-01', region: config.Auth.region, params: {Bucket: config.ImageBucket} }); let PhotoPicker = document.getElementById('PhotoPicker'); let files = PhotoPicker.files; if (!files.length) { return showError('You need to select photos to upload.'); } let prefix = (new Date()).toISOString() + '/'; let UploadList = document.getElementById('UploadList'); for(let file of files) { let photoKey = prefix + file.name; let upload = document.createElement("div"); upload.innerText = photoKey; upload.classList.add('upload'); UploadList.appendChild(upload); s3.upload({ Key: photoKey, Body: file, }, (err, data) => { if (err) { upload.classList.add('error'); console.dir(err); return showError("Error Uploading ", err.message); } upload.classList.add('uploaded'); console.log("uploaded ", photoKey); }); } PhotoPicker.value = ""; } }); } function onLoad() { let LoginPane = document.getElementById('LoginPane'); let ErrorPane = document.getElementById('ErrorPane'); let ContentPane = document.getElementById('ContentPane'); let authData = { ClientId: config.Auth.userPoolWebClientId, AppWebDomain : config.Auth.authDomain, TokenScopesArray : config.Auth.tokenScopes, RedirectUriSignIn : 'https://hdr.nd.gl', RedirectUriSignOut : 'https://hdr.nd.gl', IdentityProvider : config.Auth.identityProvider, UserPoolId : config.Auth.userPoolId, AdvancedSecurityDataCollectionFlag : false }; auth = new AmazonCognitoIdentity.CognitoAuth(authData); auth.userhandler = { onSuccess: function(result) { creds = result; LoginPane.classList.add('loggedin'); ContentPane.classList.add('loggedin'); ErrorPane.classList.remove('error'); }, onFailure: function(err) { showError(err); } }; var curUrl = window.location.href; auth.parseCognitoWebResponse(curUrl); document.getElementById('BtnLogin').onclick = () => { auth.getSession(); }; auth.getSession(); document.getElementById('BtnUpload').onclick = () => { upload(); }; } var onReady = new Promise((resolve, reject) => { if(document.readyState === "complete" || document.readyState === "loaded" || document.readyState === "interactive") { resolve(document); } else { document.addEventListener('DOMContentLoaded', () => { resolve(document); }, false); } }); onReady.then(onLoad); }());