'use strict' var baseAPIURL = "https://v1fdqlovbi.execute-api.us-west-2.amazonaws.com/dev/"; var poolData = { UserPoolId: 'us-west-2_SboavMmyJ', ClientId: '3eleu306105d5ic4u6bk6utkvq' }; var cognitoCredentials = { IdentityPoolId: 'us-west-2:51f010d3-8fd2-4f7e-b062-1a305c0d9e17' }; var token; d("#new").innerHTML = 'new'; function xhr(payload, url, xtoken, hash, onComplete) { var xhr=j(); if(payload) { xhr.open("PUT", url); xhr.setRequestHeader("Content-Type", "application/json"); } else { xhr.open("GET", url); } if(xtoken) { xhr.withCredentials = true; xhr.setRequestHeader("Authorization", xtoken); } if(hash) { xhr.integrity = "ni:///sha-256;" + hash + ""; } xhr.onreadystatechange=function() { if(xhr.readyState==4) if(xhr.status >= 200 && xhr.status < 400) onComplete(xhr.responseText); else onComplete(null); } if(payload) { xhr.send(JSON.stringify(payload)); } else { xhr.send(); } } function checkUser() { var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData); var cognitoUser = userPool.getCurrentUser(); if (cognitoUser) { cognitoUser.getSession(function (err, session) { token = session.getIdToken().getJwtToken(); cognitoCredentials["Logins"] = { "cognito-idp.us-west-2.amazonaws.com/us-west-2_SboavMmyJ": token }; }); d("#login").innerHTML = '' + cognitoUser.getUsername() + ''; } else { d("#login").innerHTML = 'login'; } AWSCognito.config.region = "us-west-2"; AWSCognito.config.credentials = new AWSCognito.CognitoIdentityCredentials(cognitoCredentials); AWSCognito.config.credentials.get(function () {}) } function tagColor(tag) { switch(tag) { case "js": case "javascript": return "post-category-js"; case "fun": case "editor": case "python": return "post-category-pure"; case "design": case "swag": return "post-category-design"; default: } return "post-category-yui"; } function getPosts(postId) { var URL = baseAPIURL; if(postId) { URL += postId; console.log(URL); } xhr(null, URL, token, null, function(results) { if(!results) return console.log("error"); var response = JSON.parse(results); var data = response["Items"]; d("#subhead-category").innerHTML = 'Interesting Stuff ....'; d("#posts").innerHTML = ''; for (var i = 0; i < data.length; i++) { d("#posts").innerHTML += '

'+data[i]["title"]+'

'+data[i]["content"]+'

' } if(postId) d("#footer").innerHTML = '
'; else d("#footer").innerHTML = ''; FB.XFBML.parse(); }); } function new_login() { xhr(null, "/login.html", token, "fakehashshouldnotwork", function(results) { if(!results) return console.log("error"); d("#activity").innerHTML = results; }) } function new_logout() { xhr(null, "/logout.html", token, "fakehashshouldnotwork", function(results) { if(!results) return console.log("error"); d("#activity").innerHTML = results; }) } function myLogout() { var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData); var cognitoUser = userPool.getCurrentUser(); if (cognitoUser) { cognitoUser.signOut(); } cancelEditor(); checkUser(); } function new_item() { var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData); var cognitoUser = userPool.getCurrentUser(); xhr(null, "/editor.html", token, "fakehashshouldnotwork", function(results) { if(!results) return console.log("error"); d("#activity").innerHTML = results; if(cognitoUser) d("#author").innerHTML = cognitoUser.getUsername(); }) } function myLogin() { var authenticationData = { Username: d('#user').value, Password: d('#pass').value, }; var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData); var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData); var userData = { Username: authenticationData.Username, Pool: userPool, }; var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData); cognitoUser.authenticateUser(authenticationDetails, { onSuccess: function (result) { cancelEditor(); checkUser(); }, onFailure: function (err) { alert(err); }, mfaRequired: function(codeDeliveryDetails) { cognitoUser.sendMFACode(mfaCode, this) }, newPasswordRequired: function(userAttributes, requiredAttributes) { delete userAttributes.email_verified; try { var newPassword = d("#newpassinput").value; } catch(e) { d("#newpass").innerHTML = ''; } if (newPassword) cognitoUser.completeNewPasswordChallenge(newPassword, userAttributes, this); } }); } function cancelEditor() { d("#activity").innerHTML = ''; } function post_put() { if(!token) return; var newPost = { "title": d("#title").textContent, "tags": d("#tags").textContent, "author": d("#author").textContent, "content": d("#content").textContent, } xhr(newPost, baseAPIURL + "posts", token, null, function(results) { cancelEditor(); if(results) getPosts(null); }); } function introspect() { xhr(null, baseAPIURL + "introspect", token, null, function(results){console.log(JSON.stringify(results))} ); } window.onload=function(e) { checkUser(); introspect(); if(window.location.hash) { var postId = window.location.hash.substring(1); getPosts(postId); } else { getPosts(null); } };