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", credentials.accessKeyId) localStorage.setItem("secretAccessKey", credentials.secretAccessKey) localStorage.setItem("sessionToken", 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').innerText(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) } else if (accessKeyId !== null && secretAccessKey !== null && sessionToken !== null) { // user is signed in setSignInDisplay(true) } // 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) } $(document).ready(function() { refreshSignedInStatus() })