-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfirebase.js
80 lines (71 loc) · 1.84 KB
/
firebase.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
import firebase from "firebase/compat";
import {
getFirestore,
collection,
getDocs,
onSnapshot,
addDoc,
deleteDoc,
doc,
orderBy,
serverTimestamp,
getDoc,
query,
where,
} from "firebase/firestore";
// ------------- PRIMARY --------------
// const firebaseConfig = {
// apiKey: "AIzaSyAwJNaV_7u-v-IebeaPaFNPxbT8D1AmUd0",
// authDomain: "chewsy2-296c9.firebaseapp.com",
// projectId: "chewsy2-296c9",
// storageBucket: "chewsy2-296c9.appspot.com",
// messagingSenderId: "589371967540",
// appId: "1:589371967540:web:40a7d8e7363fe5cc75a261",
// measurementId: "G-Y9SMGW9NJT"
// };
// ------------- BACKUP --------------
const firebaseConfig = {
// chewsy 2
apiKey: "AIzaSyAwJNaV_7u-v-IebeaPaFNPxbT8D1AmUd0",
authDomain: "chewsy2-296c9.firebaseapp.com",
projectId: "chewsy2-296c9",
storageBucket: "chewsy2-296c9.appspot.com",
messagingSenderId: "589371967540",
appId: "1:589371967540:web:40a7d8e7363fe5cc75a261",
measurementId: "G-Y9SMGW9NJT",
};
// Initialize Firebase
let app;
if (firebase.apps.length === 0) {
app = firebase.initializeApp(firebaseConfig);
} else {
app = firebase.app();
}
// init services
const auth = firebase.auth();
const db = getFirestore();
// collection ref
const colRef = query(collection(db, "users"), orderBy("email"));
// getting all users
let allUsers;
onSnapshot(colRef, (docSnap) => {
allUsers = [];
docSnap.forEach((doc) => {
allUsers.push({ ...doc.data(), id: doc.id });
});
});
// get current user data
let user;
const getUser = async () => {
if (auth.currentUser) {
const docRef = doc(db, "users", auth.currentUser.uid);
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
user = { ...docSnap.data(), id: docSnap.id };
} else {
console.log("No such document!");
}
}
};
getUser();
export { auth, db, user, getUser, allUsers };