-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
44 lines (34 loc) · 1.28 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
var username = document.getElementById('username');
var message = document.getElementById('message');
var messages = document.getElementById('messages');
var form = document.getElementById('form');
var login = document.getElementById('login');
var baseRef = new Firebase("https://gdgfbcodelab.firebaseIO.com");
var messagesRef = baseRef.child("messages");
// Retrieve new posts as they are added to Firebase
messagesRef.on("child_added", function(snapshot) {
var newPost = snapshot.val();
var msg = document.createElement("div");
msg.innerText = newPost.username + ": " + newPost.message;
messages.appendChild(msg);
console.log(newPost);
});
form.addEventListener('submit', function(event) {
event.preventDefault();
console.log("form submitted");
messagesRef.push({'username':username.value,'message':message.value});
message.value = "";
});
login.addEventListener('click', function(event) {
baseRef.authWithOAuthPopup("google", function(error, authData) {
if (error) {
console.log("Login Failed!", error);
} else {
console.log("Authenticated successfully with payload:", authData);
username.value = authData.google.displayName;
username.disabled = true;
form.style.display = "block";
login.style.display = "none";
}
});
});