Mercurial > hg > bitsyauth
comparison example/persona.html @ 48:d66d6c10cfe4
refactor to .js file
| author | Jeff Hammel <jhammel@mozilla.com> |
|---|---|
| date | Mon, 30 Dec 2013 15:49:19 -0800 |
| parents | 54a53bbe5be9 |
| children | 8358634e3d75 |
comparison
equal
deleted
inserted
replaced
| 47:f2474ffcee96 | 48:d66d6c10cfe4 |
|---|---|
| 8 <meta http-equiv="X-UA-Compatible" content="IE=Edge"> | 8 <meta http-equiv="X-UA-Compatible" content="IE=Edge"> |
| 9 | 9 |
| 10 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> | 10 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> |
| 11 <script src="https://login.persona.org/include.js"></script> | 11 <script src="https://login.persona.org/include.js"></script> |
| 12 <script> | 12 <script> |
| 13 var nclicks = 0; | |
| 14 | |
| 15 $(document).ready(function() { | 13 $(document).ready(function() { |
| 16 | 14 |
| 17 var currentUser = null; | 15 var currentUser = null; |
| 18 | 16 |
| 19 navigator.id.watch({ | |
| 20 loggedInUser: currentUser, | |
| 21 onlogin: function(assertion) { | |
| 22 // A user has logged in! Here you need to: | |
| 23 // 1. Send the assertion to your backend for verification and to create a session. | |
| 24 // 2. Update your UI. | |
| 25 $.ajax({type: 'POST', | |
| 26 url: '/auth/login', // This is a URL on your website. | |
| 27 data: {assertion: assertion}, | |
| 28 success: function(res, status, xhr) { window.location.reload(); }, | |
| 29 error: function(xhr, status, err) { | |
| 30 navigator.id.logout(); | |
| 31 alert("Login failure: " + err); | |
| 32 } | |
| 33 }); | |
| 34 }, | |
| 35 onlogout: function() { | |
| 36 // A user has logged out! Here you need to: | |
| 37 // Tear down the user's session by redirecting the user or making a call to your backend. | |
| 38 // Also, make sure loggedInUser will get set to null on the next page load. | |
| 39 // (That's a literal JavaScript null. Not false, 0, or undefined. null.) | |
| 40 // $.ajax({ | |
| 41 // type: 'POST', | |
| 42 // url: '/auth/logout', // This is a URL on your website. | |
| 43 // success: function(res, status, xhr) { window.location.reload(); }, | |
| 44 // error: function(xhr, status, err) { alert("Logout failure: " + err); } | |
| 45 // }); | |
| 46 } | |
| 47 }); | |
| 48 | |
| 49 | |
| 50 var signinLink = document.getElementById('signin'); | |
| 51 if (signinLink) { | |
| 52 signinLink.onclick = function() { | |
| 53 navigator.id.request(); | |
| 54 nclicks += 1; | |
| 55 $('#clickcounter').append('<li>This is click ' + nclicks + '</li>'); | |
| 56 }; | |
| 57 } | |
| 58 | |
| 59 var signoutLink = document.getElementById('signout'); | |
| 60 if (signoutLink) { | |
| 61 signoutLink.onclick = function() { navigator.id.logout(); }; | |
| 62 } | |
| 63 }); | |
| 64 </script> | 17 </script> |
| 65 </head> | 18 </head> |
| 66 | 19 |
| 67 <body> | 20 <body> |
| 68 <h1> | 21 <h1> |
