From f4b867aa4aa5ed036820d483295dd043370a5b66 Mon Sep 17 00:00:00 2001
From: Ben Adida <ben@adida.net>
Date: Tue, 12 Jul 2011 12:23:19 -0700
Subject: [PATCH] most paths now work

---
 .../dialog/controllers/dialog_controller.js   | 102 +++++++++++++++++-
 .../static/dialog/dialog/views/addemail.ejs   |   9 ++
 .../dialog/dialog/views/bottom-addemail.ejs   |   3 +
 .../dialog/views/bottom-confirmemail.ejs      |   2 +
 .../dialog/dialog/views/confirmemail.ejs      |  13 +++
 .../static/dialog/dialog/views/signin.ejs     |   4 +-
 6 files changed, 130 insertions(+), 3 deletions(-)
 create mode 100644 browserid/static/dialog/dialog/views/addemail.ejs
 create mode 100644 browserid/static/dialog/dialog/views/bottom-addemail.ejs
 create mode 100644 browserid/static/dialog/dialog/views/bottom-confirmemail.ejs
 create mode 100644 browserid/static/dialog/dialog/views/confirmemail.ejs

diff --git a/browserid/static/dialog/dialog/controllers/dialog_controller.js b/browserid/static/dialog/dialog/controllers/dialog_controller.js
index c30925dd0..46cd4be18 100644
--- a/browserid/static/dialog/dialog/controllers/dialog_controller.js
+++ b/browserid/static/dialog/dialog/controllers/dialog_controller.js
@@ -55,11 +55,58 @@ $.Controller("Dialog", {}, {
       var assertion = CryptoStubs.createAssertion(audience, email, privkey, issuer);
       this.onsuccess(assertion);
     },
+
+    "#addemail click": function(event) {
+      this.doNewEmail();
+    },
+
+    "#addemail_button click": function(event) {
+      // add the actual email
+      // now we need to actually try to stage the creation of this account.
+      var email = $("#email_input").val();
+      var keypair = CryptoStubs.genKeyPair();
+
+      var self = this;
+
+      // kick the user to waiting/status page while we talk to the server.
+      this.doWait(
+        "One Moment Please...",
+        "We're adding this email to your account, this should only take a couple seconds."
+      );
+
+      $.ajax({
+        url: '/wsapi/add_email?email=' + encodeURIComponent(email)
+              + '&pubkey=' + encodeURIComponent(keypair.pub)
+              + '&site=' + encodeURIComponent(this.remoteOrigin.replace(/^(http|https):\/\//, '')),
+        success: function() {
+          // email successfully staged, now wait for email confirmation
+          self.doConfirmEmail(email, keypair);
+        },
+        error: function() {
+          runErrorDialog(
+            "serverError",
+            "Error Adding Address!",
+            "There was a technical problem while trying to add this email to your account.  Yucky.");
+        }
+      });
+    },
+
+    "#notme click": function(event) {
+      clearEmails();
+      var self = this;
+      $.get("/wsapi/logout", function() {
+          self.doAuthenticate();
+      });
+    },
             
     "#cancel click": function(event) {
       this.onerror("canceled");
     },
 
+    "#continue_button click": function(event) {
+      this.doSignIn();
+    },
+
     getVerifiedEmail: function(remoteOrigin, onsuccess, onerror) {
       // check to see if there's any pubkeys stored in the browser
       var haveIDs = _.keys(getEmails()).length > 0;
@@ -98,6 +145,59 @@ $.Controller("Dialog", {}, {
       $('#dialog').html("views/wait.ejs", {title: title, message: message});
     },
 
+    doNewEmail: function() {
+      $('#dialog').html("views/addemail.ejs", {});
+      $('#bottom-bar').html("views/bottom-addemail.ejs", {});
+    },
+
+    doConfirmEmail: function(email, keypair) {
+      $('#dialog').html("views/confirmemail.ejs", {email:email});
+      $('#bottom-bar').html("");
+
+      var self = this;
+
+      // now poll every 3s waiting for the user to complete confirmation
+      function setupRegCheck() {
+        return setTimeout(function() {
+            $.ajax({
+                url: '/wsapi/registration_status',
+                  success: function(status, textStatus, jqXHR) {
+                  // registration status checks the status of the last initiated registration,
+                  // it's possible return values are:
+                  //   'complete' - registration has been completed
+                  //   'pending'  - a registration is in progress
+                  //   'noRegistration' - no registration is in progress
+                  if (status === 'complete') {
+                    // this is a secondary registration from browserid.org, persist
+                    // email, keypair, and that fact
+                    self.persistAddressAndKeyPair(email, keypair, "browserid.org:443");
+                    
+                    // and tell the user that everything is really quite awesome.
+                    self.find("#waiting_confirmation").hide();
+                    self.find("#confirmed_notice").show();
+                    self.find('#bottom-bar').html("views/bottom-confirmemail.ejs", {});
+                  } else if (status === 'pending') {
+                    // try again, what else can we do?
+                    pollTimeout = setupRegCheck();
+                  } else {
+                    runErrorDialog("serverError",
+                                   "Registration Failed",
+                                   "An error was encountered and the sign up cannot be completed, please try again later.");
+                  }
+                },
+                  error: function(jqXHR, textStatus, errorThrown) {
+                  runErrorDialog("serverError", "Registration Failed", jqXHR.responseText);
+                }
+              });
+          }, 3000);
+      }
+      
+      // setup the timeout
+      this.pollTimeout = setupRegCheck();
+
+      // FIXME cancel this timeout appropriately on cancel
+    },
+
     persistAddressAndKeyPair: function(email, keypair, issuer) {
       var new_email_obj= {
         created: new Date(),
@@ -145,7 +245,7 @@ $.Controller("Dialog", {}, {
             
             function addNextEmail() {
               if (!emailsToAdd || !emailsToAdd.length) {
-                doSignIn();
+                self.doSignIn();
                 return;
               }
 
diff --git a/browserid/static/dialog/dialog/views/addemail.ejs b/browserid/static/dialog/dialog/views/addemail.ejs
new file mode 100644
index 000000000..14a6ddebf
--- /dev/null
+++ b/browserid/static/dialog/dialog/views/addemail.ejs
@@ -0,0 +1,9 @@
+  <div class="title"> Add a new email address </div>
+  <div class="content">
+    <div class="summary">Setting up a up a new email address is easy, tell us what it is and we'll get started:</div>
+    <div class="input">
+      <div class="label"> Email </div>
+      <div class="input"> <input type="text" id="email_input"></input></div>
+      <div class="note"></div>
+    </div>
+  </div>
diff --git a/browserid/static/dialog/dialog/views/bottom-addemail.ejs b/browserid/static/dialog/dialog/views/bottom-addemail.ejs
new file mode 100644
index 000000000..dd6467895
--- /dev/null
+++ b/browserid/static/dialog/dialog/views/bottom-addemail.ejs
@@ -0,0 +1,3 @@
+  <button id="back">Go Back</button>
+  <button id="addemail_button" class="righty action">Add Email</button>
+  <button id="cancel" class="righty">Cancel</button>
diff --git a/browserid/static/dialog/dialog/views/bottom-confirmemail.ejs b/browserid/static/dialog/dialog/views/bottom-confirmemail.ejs
new file mode 100644
index 000000000..0c7bfbc71
--- /dev/null
+++ b/browserid/static/dialog/dialog/views/bottom-confirmemail.ejs
@@ -0,0 +1,2 @@
+<button class="righty action" id="continue_button">Continue</button>
+<button class="righty" id="cancel">Cancel</button>
diff --git a/browserid/static/dialog/dialog/views/confirmemail.ejs b/browserid/static/dialog/dialog/views/confirmemail.ejs
new file mode 100644
index 000000000..e4772b62c
--- /dev/null
+++ b/browserid/static/dialog/dialog/views/confirmemail.ejs
@@ -0,0 +1,13 @@
+  <div class="title"> Confirm Your Email </div>
+  <div class="content">
+    <div class="summary">Welcome, <span class="email good"><%= email %></span>.  You will receive a <strong>confirmation email</strong> in a few moments.  To activate your BrowserID, please <strong>visit the link</strong> included in the email.</div>
+    <div class="attention" id="waiting_confirmation">
+      Waiting for email confirmation...
+    </div>
+    <div class="attention_awesome" style="display:none;" id="confirmed_notice">
+      Your email has been confirmed! </span>
+    </div>
+    <div class="actions">
+      <div class="action"> No email yet?  <a href="#">Resend it!</a></div>
+    </div>
+  </div>
diff --git a/browserid/static/dialog/dialog/views/signin.ejs b/browserid/static/dialog/dialog/views/signin.ejs
index c02b1e69d..b39509914 100644
--- a/browserid/static/dialog/dialog/views/signin.ejs
+++ b/browserid/static/dialog/dialog/views/signin.ejs
@@ -7,6 +7,6 @@
     </form>
   </div>
   <div class="actions">
-    <div class="action"><a href="#">Add a new email address</a></div>
-    <div class="action"><a href="#">This is not me.</a></div>
+    <div class="action"><a id="addemail" href="#">Add a new email address</a></div>
+    <div class="action"><a id="notme" href="#">This is not me.</a></div>
   </div>
-- 
GitLab