forked from gdroberts/rashomon
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrashomon.auth.js
109 lines (89 loc) · 2.63 KB
/
rashomon.auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var auth = {};
$(document).ready(function () {
$("#auth").toggle();
$('.signin').click(function () {
navigator.id.request();
});
$("#logout").click(function() {
navigator.id.logout();
});
});
function loggedIn(email) {
setSessions([{
email: email
}]);
var sign = $("#signin");
sign.text("Signed in as " + email);
sign.unbind("click");
$("#auth").toggle();
$("#upload").toggle();
}
function setSessions(val) {
if (navigator.id) {
navigator.id.sessions = val ? val : [];
}
}
function loggedOut() {
console.log("logged out");
}
navigator.id.watch({
loggedInUser: null,
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({ /* <-- This example uses jQuery, but you can use whatever you'd like */
type: 'POST',
url: 'signin.php', // This is a URL on your website.
dataType: 'JSON',
data: {assertion: assertion},
success: function(res, status, xhr) {
console.log(res.status);
if (res.status === "okay"){
auth.assertion = assertion;
Rashomon.loggedIn();
} else {
$("#auth").toggle();
}
},
error: function(xhr, status, err) { 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: 'signin.php', // This is a URL on your website.
success: function(res, status, xhr) { window.location.reload(); },
error: function(xhr, status, err) { alert("Logout failure: " + err); }
});
}
});
//browserID assertion
function gotAssertion(assertion) {
// got an assertion, now send it up to the server for verification
if (assertion !== null) {
$.ajax({
type: 'POST',
url: 'signin.php',
data: {
assertion: assertion
},
success: function (res, status, xhr) {
if (res === null) {
loggedOut();
} else {
loggedIn(res);
}
},
error: function(xhr, status, err) {
alert("login failure" + err);
}
});
} else {
loggedOut();
}
}