-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
119 lines (107 loc) · 3.59 KB
/
index.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
// Import stylesheets
import './style.css';
// Firebase App (the core Firebase SDK) is always required and must be listed first
import * as firebase from "firebase/app";
// Add the Firebase products that you want to use
import "firebase/auth";
import "firebase/firestore";
import * as firebaseui from 'firebaseui';
// Document elements
const startRsvpButton = document.getElementById('startRsvp');
const guestbookContainer = document.getElementById('guestbook-container');
const form = document.getElementById('leave-message');
const input = document.getElementById('message');
const guestbook = document.getElementById('guestbook');
const numberAttending = document.getElementById('number-attending');
const rsvpYes = document.getElementById('rsvp-yes');
const rsvpNo = document.getElementById('rsvp-no');
var rsvpListener = null;
var guestbookListener = null;
async function main() {
// Add Firebase project configuration object here
var firebaseConfig = {
apiKey: "AIzaSyAvkqPeroJe0z_EOe0NuRsqIDss-4Bc3UY",
authDomain: "meet-hirwa.firebaseapp.com",
databaseURL: "https://meet-hirwa-default-rtdb.firebaseio.com",
projectId: "meet-hirwa",
storageBucket: "meet-hirwa.appspot.com",
messagingSenderId: "667940476743",
appId: "1:667940476743:web:f58dbecc741238612b5e62",
measurementId: "G-Y3W39K2B07"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
// var firebaseConfig = {};
// firebase.initializeApp(firebaseConfig);
// FirebaseUI config
const uiConfig = {
credentialHelper: firebaseui.auth.CredentialHelper.NONE,
signInOptions: [
// Email / Password Provider.
firebase.auth.EmailAuthProvider.PROVIDER_ID
],
callbacks: {
signInSuccessWithAuthResult: function(authResult, redirectUrl) {
// Handle sign-in.
// Return false to avoid redirect.
return false;
}
}
};
const ui = new firebaseui.auth.AuthUI(firebase.auth());
startRsvpButton.addEventListener("click",
() => {
ui.start("#firebaseui-auth-container", uiConfig);
});
firebase.auth().onAuthStateChanged((user)=> {if (user){
startRsvpButton.textContent = "LOGOUT";
// Show guestbook to logged-in users
guestbookContainer.style.display = "block";
}
else{
startRsvpButton.textContent = "RSVP";
// Hide guestbook for non-logged-in users
guestbookContainer.style.display = "none";
}
});
startRsvpButton.addEventListener("click",
() => {
if (firebase.auth().currentUser) {
// User is signed in; allows user to sign out
firebase.auth().signOut();
} else {
// No user is signed in; allows user to sign in
ui.start("#firebaseui-auth-container", uiConfig);
}
});
form.addEventListener("submit", (e) => {
// Prevent the default form redirect
e.preventDefault();
// Write a new message to the database collection "guestbook"
firebase.firestore().collection("guestbook").add({
text: input.value,
timestamp: Date.now(),
name: firebase.auth().currentUser.displayName,
userId: firebase.auth().currentUser.uid
})
// clear message input field
input.value = "";
// Return false to avoid redirect
return false;
});
firebase.firestore().collection("guestbook")
.orderBy("timestamp","desc")
.onSnapshot((snaps) => {
// Reset page
guestbook.innerHTML = "";
// Loop through documents in database
snaps.forEach((doc) => {
// Create an HTML entry for each document and add it to the chat
const entry = document.createElement("p");
entry.textContent = doc.data().name + ": " + doc.data().text;
guestbook.appendChild(entry);
});
});
}
main();
// Initialize the FirebaseUI widget using Firebase