annotate bitsyauth/js/persona.js @ 50:5517ac16c9fb

comment+whitespace
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 30 Dec 2013 22:55:29 -0800
parents 8358634e3d75
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
48
d66d6c10cfe4 refactor to .js file
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 // setup Mozilla persona
d66d6c10cfe4 refactor to .js file
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 // requires: jquery
d66d6c10cfe4 refactor to .js file
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3
49
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
4 function persona(loggedInUser, onloginURL, onlogoutURL) {
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
5
48
d66d6c10cfe4 refactor to .js file
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
49
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
7 navigator.id.watch({loggedInUser: loggedInUser,
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
8 onlogin: function(assertion) {
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
9 // A user has logged in! Here you need to:
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
10 // 1. Send the assertion to your backend for verification and to create a session.
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
11 // 2. Update your UI.
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
12 $.ajax({type: 'POST',
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
13 url: onloginURL, // This is a URL on your website.
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
14 data: {assertion: assertion},
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
15 success: function(res, status, xhr) { window.location.reload(); },
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
16 error: function(xhr, status, err) {
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
17 navigator.id.logout();
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
18 alert("Login failure: " + err);
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
19 }
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
20 });
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
21 },
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
22 onlogout: function() {
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
23 // A user has logged out! Here you need to:
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
24 // Tear down the user's session by redirecting the user or making a call to your backend.
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
25 // Also, make sure loggedInUser will get set to null on the next page load.
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
26 // (That's a literal JavaScript null. Not false, 0, or undefined. null.)
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
27 $.ajax({type: 'POST',
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
28 url: onlogoutURL, // This is a URL on your website.
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
29 success: function(res, status, xhr) { window.location.reload(); },
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
30 error: function(xhr, status, err) {
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
31 alert("Logout failure: " + err);
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
32 }
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
33 });
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
34 }
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
35 });
8358634e3d75 bitsyauth/js/persona.js example/persona.html
Jeff Hammel <jhammel@mozilla.com>
parents: 48
diff changeset
36
50
5517ac16c9fb comment+whitespace
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
37 // add click handlers to signin, signout buttons
48
d66d6c10cfe4 refactor to .js file
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 var signinLink = document.getElementById('signin');
d66d6c10cfe4 refactor to .js file
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 if (signinLink) {
d66d6c10cfe4 refactor to .js file
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 signinLink.onclick = function() {
d66d6c10cfe4 refactor to .js file
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 navigator.id.request();
d66d6c10cfe4 refactor to .js file
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 };
d66d6c10cfe4 refactor to .js file
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 }
d66d6c10cfe4 refactor to .js file
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 var signoutLink = document.getElementById('signout');
d66d6c10cfe4 refactor to .js file
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 if (signoutLink) {
50
5517ac16c9fb comment+whitespace
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
46 signoutLink.onclick = function() {
5517ac16c9fb comment+whitespace
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
47 navigator.id.logout();
5517ac16c9fb comment+whitespace
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
48 };
48
d66d6c10cfe4 refactor to .js file
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 }
d66d6c10cfe4 refactor to .js file
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 }