/
main.js
139 lines (125 loc) · 3.72 KB
/
main.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var browseridArguments = {
// display our tos and privacy policy in the browserid dialog
privacyURL: '/privacy.html',
tosURL: '/tos.html',
siteName: 'My Favorite Beer'
};
// when the user is found to be logged in we'll update the UI, fetch and
// display the user's favorite beer from the server, and set up handlers to
// wait for user input (specifying their favorite beer).
function loggedIn(user, immediate) {
// set the user visible display
var l = $("header .login").removeClass('clickable');;
l.empty();
l.css('opacity', '1');
l.append($("<span>").text("Yo, "))
.append($("<span>").text(user.email).addClass("username"))
.append($("<span>!</span>"));
l.append($('<div><a id="logout" href="#" >(logout)</a></div>'));
l.unbind('click');
$("#logout").bind('click', logout);
if (immediate) {
$("#content .intro").hide();
$("#content .business").fadeIn(300);
}
else {
$("#content .intro").fadeOut(700, function() {
$("#content .business").fadeIn(300);
});
}
// enter causes us to save the value and do a little animation
$('input').keypress(function(e){
if(e.which == 13) {
save(e);
}
});
$("#save").click(save);
$.ajax({
type: 'GET',
url: '/api/get',
success: function(res, status, xhr) {
$("input").val(res);
}
});
// get an avatar cause it's pretty
$("<img>").attr('src', user.avatar).appendTo($("header .picture"));
}
function save(event) {
event.preventDefault();
var input = $("#content input")
input.fadeTo(200, 0);
$.ajax({
type: 'POST',
url: '/api/set',
data: { beer: input.val() },
success: function(res, status, xhr) {
input.stop(true,true);
input.fadeTo(400, 1);
}
});
}
// when the user clicks logout, we'll make a call to the server to clear
// our current session.
function logout(event) {
event.preventDefault();
$.ajax({
type: 'POST',
url: '/api/logout',
success: function() {
// and then redraw the UI.
loggedOut();
}
});
}
// when no user is logged in, we'll display a "sign-in" button
// which will call into browserid when clicked.
function loggedOut() {
$("input").val("");
$("#content .business").hide();
$('.intro').fadeIn(300);
$("header .picture").empty();
var l = $("header .login").removeClass('clickable');
l.html('<img src="i/persona_sign_in_blue.png" alt="Sign in">')
.show().one('click', function() {
$("header .login").css('opacity', '0.5');
navigator.id.get(gotVerifiedEmail, browseridArguments);
}).addClass("clickable").css('opacity','1.0');
}
// a handler that is passed an assertion after the user logs in via the
// browserid dialog
function gotVerifiedEmail(assertion) {
// got an assertion, now send it up to the server for verification
if (assertion !== null) {
$.ajax({
type: 'POST',
url: '/api/login',
data: { assertion: assertion },
success: function(res, status, xhr) {
if (res === null) loggedOut();
else loggedIn(res);
},
error: function(xhr, status, error) {
alert("login failure " + error);
}
});
}
else {
loggedOut();
}
}
// For some reason, login/logout do not respond when bound using jQuery
if (document.addEventListener) {
document.addEventListener("login", function(event) {
$("header .login").css('opacity', '0.5');
navigator.id.get(gotVerifiedEmail, browseridArguments);
}, false);
document.addEventListener("logout", logout, false);
}
// at startup let's check to see whether we're authenticated to
// myfavoritebeer (have existing cookie), and update the UI accordingly
$(function() {
$.get('/api/whoami', function (res) {
if (res === null) loggedOut();
else loggedIn(res, true);
}, 'json');
});