-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebase.js
142 lines (125 loc) · 3.06 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
import { initializeApp } from "firebase/app";
import {
createUserWithEmailAndPassword,
getAuth,
onAuthStateChanged,
signInWithEmailAndPassword,
signOut,
} from "firebase/auth";
import {
arrayUnion,
collection,
doc,
getDoc,
getDocs,
getFirestore,
query,
setDoc,
updateDoc,
where,
} from "firebase/firestore";
import { setTheUser } from "./utils/setUser";
const firebaseConfig = {
apiKey: process.env.API_KEY,
authDomain: process.env.AUTH_DOMAIN,
projectId: process.env.PROJECT_ID,
storageBucket: process.env.STORAGE_BUCKET,
messagingSenderId: process.env.MESSAGING_SENDER_ID,
appId: process.env.APP_ID,
};
const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);
const db = getFirestore(app);
onAuthStateChanged(auth, async (user) => {
if (user) {
const getUser = await getDoc(doc(db, "users", user.uid));
let data = {
uid: user.uid,
displayName: user.displayName,
email: user.email,
...getUser.data(),
};
setTheUser(data);
} else {
setTheUser(false);
}
});
export const userRegistration = async (email, username, password) => {
const res = await getDocs(
query(collection(db, "users"), where("username", "==", username))
);
if (res.size === 0) {
const res = await createUserWithEmailAndPassword(auth, email, password);
const uid = res.user.uid;
try {
await setDoc(doc(db, "users", uid), {
uid: uid,
displayName: username,
username: username,
photoSrc: "https://source.unsplash.com/collection/928423/480x480",
followers: [],
following: [],
notifications: [],
bio: "This is my bio!",
tweets: [
{
tweet: "Hey everyone!",
likes: 111,
retweets: 55,
comments: 33,
},
{
tweet: "gm",
likes: 222,
retweets: 111,
comments: 555,
},
{
tweet: "this clone website coded by @emirmorgan :O",
likes: 333,
retweets: 222,
comments: 111,
},
],
});
return res.user;
} catch (err) {
alert("Error: " + err);
}
} else {
alert("This username already on use.");
}
};
export const userLogin = async (email, password) => {
try {
return await signInWithEmailAndPassword(auth, email, password);
} catch (error) {
alert("Error: " + error);
}
};
export const userLogout = async () => {
try {
await signOut(auth);
} catch (error) {
alert("Error: " + error);
}
};
export const sendTweet = async (user, tweet, comments, retweets, likes) => {
const userDocRef = doc(db, "users", user.uid);
await updateDoc(userDocRef, {
tweets: arrayUnion({
tweet: tweet,
comments: comments,
retweets: retweets,
likes: likes,
}),
});
};
export const editProfile = async (user, source, name, bio) => {
const userDocRef = doc(db, "users", user.uid);
await updateDoc(userDocRef, {
photoSrc: source,
displayName: name,
bio: bio,
});
};