// TODO: stub out UI functionality?
//window.setTimeout(func, delay);

PW_URL="https://2u20yskc8i.execute-api.us-east-1.amazonaws.com/prod/fetch_minecraft_secret";

function authError(errorMsg) {
   // TODO: display nicely somewhere
   alert(errorMsg);
   // TODO: reset state of UI to beginning where auth is required
}

$(function() {
   $('#start-button').click( function() {
      pw = window.prompt("Password");
      $(this).html("Logging on...");

      post_data = { password: pw };
      console.log(post_data);
      iam_access_key = ""; // TODO: don't think this is a very javascript way to do this...
      iam_secret_key = "";
      $.ajax({
         url: PW_URL,
         type: "POST",
         data: JSON.stringify(post_data),
         success: function(data) {
            // AWS returns errors this way sometimes
            if (data.User) {
               console.log('ERROR: ' + data.User);
                authError('Error logging in: ' + data.User)
            }
            // lambda's .fail returns 200 for some reason (API Gateway is probably misconfigured)
            else if (data.errorMessage) {
               console.log('ERROR: ' + data.errorMessage);
               authError(data.errorMessage);
            } else if (data.access_key_id && data.secret_access_key){
               console.log(data.access_key_id);
               console.log(data.secret_access_key);
            } else {
               console.log('Returned 200 but response is malformed: ' + data);
               authError('Internal programming error');
            }
         },
         error: function(jqXHR, status, error) {
            console.log('ERROR: ' + status + ': ' + error);
            authError('Error logging in. Try again?')
         }
      });
   });
});

@@ -1,16 +1,187 @@
+// TODO: hacking together this code for now.%0A//       when it is all working I will make a proper state machine UI out of it using promises or some library or something%0A%0A%0A
 // TODO: stub ou
@@ -235,18 +235,22 @@
 elay);%0A%0A
-PW
+LAMBDA
 _URL=%22ht
@@ -333,16 +333,303 @@
 cret%22;%0A%0A
+$(function() %7B%0A   START_BUTTON = $('#start-button');%0A%0A   START_BUTTON.click( function() %7B%0A      // callback hell right now, will make more imperitive in the future%0A      var creds = getCreds();%0A      console.log('Got creds: ' + creds.access_key_id);%0A      startServer(creds);%0A   %7D);%0A%0A   
 function
@@ -651,16 +651,19 @@
 rMsg) %7B%0A
+   
    // TO
@@ -694,16 +694,19 @@
 here%0A   
+ 
+  
 alert(er
@@ -714,16 +714,19 @@
 orMsg);%0A
+   
    // TO
@@ -787,64 +787,134 @@
 red%0A
-%7D%0A%0A$(function() %7B%0A   $('#start-button').click( function(
+   %7D%0A%0A   // Save creds in HTML5 localstorage so we don't have to prompt for the password every time%0A   function saveCreds(data
 ) %7B%0A
@@ -923,261 +923,871 @@
     
-pw = 
+if (
 window.
-prompt(%22Password%22);%0A      $(this).html(%22Logging on...%22);%0A%0A      post_data = %7B password: pw %7D;%0A      console.log(post_data);%0A      iam_access_key = %22%22; // TODO: don't think this is a very javascript way to do this...%0A      iam_secret_key = %22%22
+localStorage) %7B%0A         creds = %7B%0A            'access_key_id': data.access_key_id,%0A            'secret_access_key': data.secret_access_key%0A         %7D;%0A         localStorage.setItem('creds', JSON.stringify(creds));%0A      %7D%0A      else %7B%0A         console.log('localStorage isn%5C't supported. Can%5C't save credentials.');%0A      %7D%0A   %7D%0A%0A   // Get the IAM credentials to turn the server on. Either from localstorage%0A   // or by providing the right password to a lambda function.%0A   function getCreds() %7B%0A      START_BUTTON.html(%22Authenticating...%22);%0A      if (window.localStorage && localStorage.creds) %7B%0A         creds = JSON.parse(localStorage.creds);%0A         return creds%0A      %7D else %7B%0A         return getCredsFromLabmda();%0A      %7D%0A   %7D%0A%0A   function getCredsFromLabmda() %7B%0A      var pw = window.prompt(%22Password%22);%0A%0A      var post_data = %7B password: pw %7D
 ;%0A  
@@ -1817,10 +1817,14 @@
 rl: 
-PW
+LAMBDA
 _URL
@@ -2065,33 +2065,32 @@
 %0A               
- 
 authError('Error
@@ -2456,36 +2456,73 @@
             
-console.log(
+var creds = %7B%0A                  'access_key_id': 
 data.access_
@@ -2523,27 +2523,28 @@
 ccess_key_id
-);%0A
+,%0A  
             
@@ -2550,42 +2550,136 @@
     
-console.log(data.secret_access_key
+ 'secret_access_key': data.secret_access_key%0A               %7D;%0A               saveCreds(creds);%0A               startServer(creds
 );%0A 
@@ -3047,15 +3047,99 @@
 %7D);%0A   %7D
-);
+%0A%0A   function startServer(creds) %7B%0A      START_BUTTON.html(%22Starting server...%22);%0A   %7D
 %0A%7D);%0A

Save credentials to localStorage