function startLogin() { myWindow=window.open(auth_url + '/login?response_type=token&client_id=' + user_pool_client_id + '&redirect_uri=https://' + window.location.hostname + '/assets/auth/signin.html','','width=600,height=600') myWindow.focus() } function refreshLogin(id_token) { localStorage.setItem("id_token", id_token) AWS.config.credentials = new AWS.CognitoIdentityCredentials({ IdentityPoolId: identity_pool_id, Logins: { ['cognito-idp.' + aws_region + '.amazonaws.com/' + user_pool_id]: id_token } }) AWS.config.update({region: 'ap-southeast-2'}) AWS.config.credentials.refresh((error) => { if (error) { console.error(error) signOut() } else { localStorage.setItem("accessKeyId", AWS.config.credentials.accessKeyId) localStorage.setItem("secretAccessKey", AWS.config.credentials.secretAccessKey) localStorage.setItem("sessionToken", AWS.config.credentials.sessionToken) var now = Date.now() localStorage.setItem("expiresAt", addMinutes(now, 60)) refreshSignedInStatus() } }) } function getUserInfo() { var jwt = localStorage.getItem("id_token") var jwt_segments = jwt.split('.') var payload = JSON.parse(atob(jwt_segments[1])) return payload } function setDisplayElements() { var userInfo = getUserInfo() $('#currentUserName').text(userInfo.email) } function refreshSignedInStatus() { var accessKeyId = localStorage.getItem("accessKeyId") var secretAccessKey = localStorage.getItem("secretAccessKey") var sessionToken = localStorage.getItem("sessionToken") var expiresAt = localStorage.getItem("expiresAt") var now = Date.now() if (expiresAt !== null && expiresAt < now) { // session was there, but has expired, so force a sign out signOut() return } else if (accessKeyId === null || secretAccessKey === null || sessionToken === null) { // user is not signed in setSignInDisplay(false) return } else if (accessKeyId !== null && secretAccessKey !== null && sessionToken !== null) { // user is signed in setSignInDisplay(true) return } // if we get to here, we just don't know, so force a signed out state signOut() } function setSignInDisplay(signedIn) { if (signedIn) { setDisplayElements() $('.showSignedIn').show() $('.showSignedOut').hide() } else { $('.showSignedIn').hide() $('.showSignedOut').show() } } function signOut() { localStorage.removeItem("accessKeyId") localStorage.removeItem("secretAccessKey") localStorage.removeItem("sessionToken") localStorage.removeItem("expiresAt") localStorage.removeItem("id_token") refreshSignedInStatus() } function addMinutes(date, minutes) { return new Date(date + minutes*60000) } var apigClientFactory = {}; apigClientFactory.newClient = function (config) { var apigClient = { }; if(config === undefined) { config = { accessKey: '', secretKey: '', sessionToken: '', region: aws_region, apiKey: undefined, defaultContentType: 'application/json', defaultAcceptType: 'application/json' }; } if(config.accessKey === undefined) { config.accessKey = ''; } if(config.secretKey === undefined) { config.secretKey = ''; } if(config.apiKey === undefined) { config.apiKey = ''; } if(config.sessionToken === undefined) { config.sessionToken = ''; } if(config.region === undefined) { config.region = aws_region; } //If defaultContentType is not defined then default to application/json if(config.defaultContentType === undefined) { config.defaultContentType = 'application/json'; } //If defaultAcceptType is not defined then default to application/json if(config.defaultAcceptType === undefined) { config.defaultAcceptType = 'application/json'; } // extract endpoint and path from url var invokeUrl = api_gateway_url; var endpoint = /(^https?:\/\/[^\/]+)/g.exec(invokeUrl)[1]; var pathComponent = invokeUrl.substring(endpoint.length); var sigV4ClientConfig = { accessKey: config.accessKey, secretKey: config.secretKey, sessionToken: config.sessionToken, serviceName: 'execute-api', region: config.region, endpoint: endpoint, defaultContentType: config.defaultContentType, defaultAcceptType: config.defaultAcceptType }; var authType = 'NONE'; if (sigV4ClientConfig.accessKey !== undefined && sigV4ClientConfig.accessKey !== '' && sigV4ClientConfig.secretKey !== undefined && sigV4ClientConfig.secretKey !== '') { authType = 'AWS_IAM'; } var simpleHttpClientConfig = { endpoint: endpoint, defaultContentType: config.defaultContentType, defaultAcceptType: config.defaultAcceptType }; var apiGatewayClient = apiGateway.core.apiGatewayClientFactory.newClient(simpleHttpClientConfig, sigV4ClientConfig); apigClient.commentsPost = function (params, body, additionalParams) { if(additionalParams === undefined) { additionalParams = {}; } apiGateway.core.utils.assertParametersDefined(params, [], ['body']); var commentsPostRequest = { verb: 'post'.toUpperCase(), path: pathComponent + uritemplate('/comments').expand(apiGateway.core.utils.parseParametersToObject(params, [])), headers: apiGateway.core.utils.parseParametersToObject(params, []), queryParams: apiGateway.core.utils.parseParametersToObject(params, []), body: body }; return apiGatewayClient.makeRequest(commentsPostRequest, authType, additionalParams, config.apiKey); }; return apigClient; }; function submitComment() { var commentText = $('#commentText').text() var userInfo = getUserInfo() $('#submitButton').hide() $('#commentText').hide() $('#submittingComment').show() var apigClient = apigClientFactory.newClient(); apigClient.commentsPost({}, { "authorName": userInfo.name, "authorEmail": userInfo.email, "postUrl": window.location.pathname, "comment": commentText }, {}) .then(function(result){ //This is where you would put a success callback $('#commentSuccess').show() $('#submittingComment').hide() }).catch( function(result){ //This is where you would put an error callback $('#commentError').show() $('#submittingComment').hide() }); } $(document).ready(function() { refreshSignedInStatus() $('#commentSuccess').hide() $('#commentError').hide() $('#submittingComment').hide() })