-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathadmin.js
57 lines (50 loc) · 1.47 KB
/
admin.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
"use strict";
/*global firebase */
/*global $ */
/**
* Singleton for admin page
*/
var helloAdmin = {
/**
* Initialize admin page
*/
init: function () {
firebase.auth().onAuthStateChanged(function(user) {
if (user === null) {
$("#loginPanel").show();
$("#logoutPanel").hide();
} else {
firebase.database().ref('admins/' + user.uid).once('value').then(function(result) {
if(result.val()) {
$("#loginPanel").hide();
$("#logoutPanel").show();
$("#userInfo").html(firebase.auth().currentUser.email);
} else {
firebase.auth().signOut();
$("#loginFailed").show();
$("#loginError").html("You are not an admin!");
}
}).catch(function(err) {
firebase.auth().signOut();
$("#loginFailed").show();
$("#loginError").html("Failed to check admin status!");
});
}
});
$("#logoutButton").click(function () {
firebase.auth().signOut();
$("#loginPanel").show();
$("#logoutPanel").hide();
});
$("#loginForm").submit(function (e) {
e.preventDefault();
var userEmail = $("#userEmail").val();
var userPassword = $("#userPassword").val();
firebase.auth().signInWithEmailAndPassword(userEmail, userPassword).catch(function (error) {
$("#loginFailed").show();
$("#loginError").html(error.message);
});
return false;
});
}
};