-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebase.js
More file actions
129 lines (116 loc) · 3.23 KB
/
firebase.js
File metadata and controls
129 lines (116 loc) · 3.23 KB
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
import { initializeApp } from "firebase/app";
import {
collection,
getDocs,
getFirestore,
query,
where,
addDoc,
getDoc,
doc,
setDoc,
} from "firebase/firestore";
import {
getAuth,
GoogleAuthProvider,
signInWithPopup,
signInWithEmailAndPassword,
createUserWithEmailAndPassword,
sendPasswordResetEmail,
signOut,
} from "firebase/auth";
import { getDatabase, ref, set } from "firebase/database";
import moment from "moment";
const firebaseConfig = {
apiKey: "AIzaSyDAhYlyUV1n4ZUkEZrTgeeVlmDL9jvAhZ4",
authDomain: "dev-blog-e4d2d.firebaseapp.com",
projectId: "dev-blog-e4d2d",
storageBucket: "dev-blog-e4d2d.appspot.com",
messagingSenderId: "30990206536",
appId: "1:30990206536:web:fca08d156be73c52effa7d",
measurementId: "G-S3P3RVQC3Y",
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
// Initialize Firebase Authentication and get a reference to the service
const auth = getAuth(app);
// Initialize Firestore
const db = getFirestore(app);
const provider = new GoogleAuthProvider();
// --- CRUD FUNCTIONS --- //
// all blogs GET
export async function getBlogs(db) {
const blogsCol = collection(db, "blogs");
const blogsSnapshot = await getDocs(blogsCol);
const blogsList = blogsSnapshot.docs.map((doc) => doc.data());
const sortedBlogsList = blogsList.sort((a, b) =>
moment(b.date_posted).diff(moment(a.date_posted))
);
// return blogsList;
return sortedBlogsList;
}
// one blog GET
export async function getSpecificBlog(db, id) {
const blogRef = doc(db, "blogs", id);
const blogSnap = await getDoc(blogRef);
const blogResult = blogSnap.data();
return blogResult;
}
// blog POST
// TODO Unimplemented
export async function writeBlog(blogObj) {
const db = getDatabase();
await set(ref(db, "blogs/" + blogObj.id), {
title: blogObj.title,
content: blogObj.content,
date_posted: blogObj.date_posted,
});
}
// all comments GET
export async function getComments(db, id) {
const q = query(
collection(db, "comments"),
where("associated_blog", "==", id)
);
const commentSnap = await getDocs(q);
return commentSnap.docs.map((doc) => doc.data());
}
// one comment POST
// TODO Unimplemented
export async function writeComment(commentObj) {
await setDoc(doc(db, "comments", crypto.randomUUID()), {
username: auth.currentUser.displayName,
profile_pic_url: commentObj.photoURL,
comment_body: commentObj.comment_body,
date_posted: commentObj.date_posted,
associated_blog: commentObj.associated_blog,
})
.then(() => alert("Posted"))
.catch((err) => console.log(err));
}
// --- GOOGLE AUTH --- //
// Proper signInWithGoogle
const signInWithGoogle = async () => {
try {
const res = await signInWithPopup(auth, provider);
const user = res.user;
const q = query(collection(db, "users"), where("uid", "==", user.uid));
const docs = await getDocs(q);
if (docs.docs.length === 0) {
await addDoc(collection(db, "users"), {
uid: user.uid,
name: user.displayName,
authProvider: "google",
email: user.email,
});
}
} catch (err) {
console.error(err);
// alert(err.message);
}
};
// Logout
const logout = async () => {
signOut(auth);
};
export { auth, db, signInWithGoogle, logout };