diff example/persona.html @ 30:0bf52646061b

example/persona.html example/persona.py setup.py
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 27 Dec 2013 11:24:23 -0800
parents 1bcb72011872
children f462dc131d57
line wrap: on
line diff
--- a/example/persona.html	Thu Dec 26 23:44:43 2013 -0800
+++ b/example/persona.html	Fri Dec 27 11:24:23 2013 -0800
@@ -10,10 +10,42 @@
 <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
 <script src="https://login.persona.org/include.js"></script>
 <script>
-
 function onload() {
 
-alert("page loaded!");
+//alert("page loaded!");
+
+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) {
@@ -28,7 +60,7 @@
 </script>
 </head>
 
-<body onload="onload()">
+<body>
 <h1>
 <a href="https://login.persona.org/">Mozilla persona</a>
 <a href="http://k0s.org/hg/bitsyauth/">bitsyauth</a>