/
main.js
129 lines (116 loc) · 3.52 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
function setSessions(val) {
if (navigator.id) {
navigator.id.sessions = val ? val : [ ];
}
}
// 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(email, immediate) {
setSessions([ { email: email } ]);
// 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(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) {
$.ajax({
type: 'POST',
url: '/api/set',
data: { beer: $("input").val() },
success: function(res, status, xhr) {
// noop
}
});
$("#content input").fadeOut(200).fadeIn(400);
e.preventDefault();
}
});
$.ajax({
type: 'GET',
url: '/api/get',
success: function(res, status, xhr) {
$("input").val(res);
}
});
// get a gravatar cause it's pretty
var iurl = 'http://www.gravatar.com/avatar/' +
Crypto.MD5($.trim(email).toLowerCase()) +
"?s=32";
$("<img>").attr('src', iurl).appendTo($("header .picture"));
}
// 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() {
setSessions();
$('.intro').fadeIn(300);
$("header .picture").empty();
var l = $("header .login").removeClass('clickable');
l.html('<img src="i/sign_in_blue.png" alt="Sign in">')
.show().click(function() {
$("header .login").css('opacity', '0.5');
navigator.id.getVerifiedEmail(gotVerifiedEmail);
}).addClass("clickable");
}
// 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
$.ajax({
type: 'POST',
url: '/api/login',
data: { assertion: assertion },
success: function(res, status, xhr) {
if (res === null) loggedOut();
else loggedIn(res);
},
error: function(res, status, xhr) {
alert("login failure" + res);
}
});
}
// 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.getVerifiedEmail(gotVerifiedEmail);
}, 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');
});