changeset 48:d66d6c10cfe4

refactor to .js file
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 30 Dec 2013 15:49:19 -0800
parents f2474ffcee96
children 8358634e3d75
files bitsyauth/js/persona.js example/persona.html
diffstat 2 files changed, 45 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bitsyauth/js/persona.js	Mon Dec 30 15:49:19 2013 -0800
@@ -0,0 +1,45 @@
+// setup Mozilla persona
+// requires: jquery
+
+function persona(loggedInUser) {
+  navigator.id.watch({
+  loggedInUser: loggedInUser,
+  onlogin: function(assertion) {
+    // A user has logged in! Here you need to:
+    // 1. Send the assertion to your backend for verification and to create a session.
+    // 2. Update your UI.
+    $.ajax({type: 'POST',
+            url: '/auth/login', // This is a URL on your website.
+            data: {assertion: assertion},
+            success: function(res, status, xhr) { window.location.reload(); },
+            error: function(xhr, status, err) {
+                navigator.id.logout();
+                alert("Login failure: " + err);
+            }
+           });
+  },
+  onlogout: function() {
+    // A user has logged out! Here you need to:
+    // Tear down the user's session by redirecting the user or making a call to your backend.
+    // Also, make sure loggedInUser will get set to null on the next page load.
+    // (That's a literal JavaScript null. Not false, 0, or undefined. null.)
+    //    $.ajax({
+    //  type: 'POST',
+    //  url: '/auth/logout', // This is a URL on your website.
+    //  success: function(res, status, xhr) { window.location.reload(); },
+    //  error: function(xhr, status, err) { alert("Logout failure: " + err); }
+    // });
+  }});
+
+  var signinLink = document.getElementById('signin');
+  if (signinLink) {
+      signinLink.onclick = function() {
+          navigator.id.request();
+      };
+  }
+
+  var signoutLink = document.getElementById('signout');
+  if (signoutLink) {
+      signoutLink.onclick = function() { navigator.id.logout(); };
+  }
+}
\ No newline at end of file
--- a/example/persona.html	Mon Dec 30 10:12:25 2013 -0800
+++ b/example/persona.html	Mon Dec 30 15:49:19 2013 -0800
@@ -10,57 +10,10 @@
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
 <script src="https://login.persona.org/include.js"></script>
 <script>
-var nclicks = 0;
-
 $(document).ready(function() {
 
 var currentUser = null;
 
-navigator.id.watch({
-  loggedInUser: currentUser,
-  onlogin: function(assertion) {
-    // A user has logged in! Here you need to:
-    // 1. Send the assertion to your backend for verification and to create a session.
-    // 2. Update your UI.
-    $.ajax({type: 'POST',
-            url: '/auth/login', // This is a URL on your website.
-            data: {assertion: assertion},
-            success: function(res, status, xhr) { window.location.reload(); },
-            error: function(xhr, status, err) {
-                       navigator.id.logout();
-                       alert("Login failure: " + err);
-       }
-    });
-  },
-  onlogout: function() {
-    // A user has logged out! Here you need to:
-    // Tear down the user's session by redirecting the user or making a call to your backend.
-    // Also, make sure loggedInUser will get set to null on the next page load.
-    // (That's a literal JavaScript null. Not false, 0, or undefined. null.)
-    //    $.ajax({
-    //  type: 'POST',
-    //  url: '/auth/logout', // This is a URL on your website.
-    //  success: function(res, status, xhr) { window.location.reload(); },
-    //  error: function(xhr, status, err) { alert("Logout failure: " + err); }
-    // });
-  }
-});
-
-
-var signinLink = document.getElementById('signin');
-if (signinLink) {
-signinLink.onclick = function() {
-navigator.id.request();
-nclicks += 1;
-$('#clickcounter').append('<li>This is click ' + nclicks + '</li>');
-};
-}
-
-var signoutLink = document.getElementById('signout');
-if (signoutLink) {
-signoutLink.onclick = function() { navigator.id.logout(); };
-}
-});
 </script>
 </head>