-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFirebase.js
More file actions
193 lines (182 loc) · 5.56 KB
/
Firebase.js
File metadata and controls
193 lines (182 loc) · 5.56 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { initializeApp } from "firebase/app";
import { getAuth, signInWithPopup, GoogleAuthProvider, signOut } from "firebase/auth";
import { getFirestore, collection, query, where, doc, setDoc, getDoc, getDocs, onSnapshot, updateDoc } from "firebase/firestore";
import { get } from 'svelte/store';
import AuthStore from "./AuthStore";
import UserInfoStore from "./UserInfoStore";
var firebaseConfig = {
apiKey: 'AIzaSyByCa-MhbzY1ghAYjgcLLOHXyu2O1WP6x8',
authDomain: 'studyhouse-74491.firebaseapp.com',
projectId: 'studyhouse-74491',
storageBucket: 'studyhouse-74491.appspot.com',
messagingSenderId: '1058200447719',
appId: '1:1058200447719:web:8923387544612014c25042',
measurementId: 'G-KTGML69YYZ'
};
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const db = getFirestore(app);
/**
* @type {import("@firebase/firestore").Unsubscribe}
*/
let unsubscribe;
async function loginWithGoogle() {
try {
const result = await signInWithPopup(auth, new GoogleAuthProvider());
const user = result.user;
try {
const uid = user.uid;
let uid_str = String(uid);
console.log(uid_str);
const docRef = doc(db, "userInfo", uid_str);
const docSnap = await getDoc(docRef);
if (!docSnap.exists()) {
try {
const newUserRef = collection(db, "userInfo");
await setDoc(doc(newUserRef, uid_str), {
"uid": uid_str,
"name": user.displayName,
"coins": 0,
"rooms": ["Gray"],
"decorations": []
});
}
catch (e) {
console.log(e)
}
}
unsubscribe = onSnapshot(doc(db, "userInfo", uid_str), (doc) => {
try {
// @ts-ignore
if (doc.data() != undefined && doc.data().uid == uid_str) {
UserInfoStore.set({
// @ts-ignore
uid: doc.data().uid,
// @ts-ignore
name: doc.data().name, // Add the user's name to the UserInfoStore
// @ts-ignore
coins: doc.data().coins,
// @ts-ignore
rooms: doc.data().rooms,
// @ts-ignore
decorations: doc.data().decorations
});
}
}
catch (e) {
console.log(e);
}
});
}
catch (e) {
console.log(e);
}
AuthStore.set({
isLoggedIn: user !== undefined && user !== null,
user: user,
firebaseControlled: true
});
}
catch (e) {
console.log(e);
}
}
async function logoutFromGoogle() {
try {
unsubscribe();
await signOut(auth);
AuthStore.set({
isLoggedIn: false,
user: undefined,
firebaseControlled: true
});
}
catch (e) {
console.log(e);
}
}
/**
* @param {any} activity
* @param {any} date
* @param {any} length
*/
async function addActivity(activity, date, length) {
try {
const uid = get(AuthStore).user?.uid;
const newActivityRef = doc(collection(db, "activities"));
await setDoc(newActivityRef, {
"uid": uid,
"activity": activity,
"date": date,
"length": length
});
await updateDoc(doc(db, "userInfo", get(UserInfoStore).uid), {
"uid": get(UserInfoStore).uid,
"coins": get(UserInfoStore).coins + Math.round(length/1000),
"rooms": get(UserInfoStore).rooms,
"decorations": get(UserInfoStore).decorations
});
}
catch (e) {
console.log(e);
}
}
async function getActivities() {
try {
const uid = get(AuthStore).user?.uid;
const q = query(collection(db, "activities"), where("uid", "==", uid));
const querySnapshot = await getDocs(q);
/**
* @type {{ activity: any; date: any; length: any; }[]}
*/
let ret = [];
querySnapshot.forEach((doc) => {
ret.push({
"activity": doc.data().activity,
"date": doc.data().date,
"length": doc.data().length
});
});
return ret;
}
catch (e) {
console.log(e);
}
}
/**
* @param {string} room
* @param {number} price
*/
async function buyRoom(room, price) {
try {
let rooms = get(UserInfoStore).rooms;
rooms.push(room);
await updateDoc(doc(db, "userInfo", get(UserInfoStore).uid), {
"uid": get(UserInfoStore).uid,
"coins": get(UserInfoStore).coins - price,
"rooms": rooms,
"decorations": get(UserInfoStore).decorations
});
}
catch (e) {
console.log(e);
}
}
export const getGoogleUserDisplayName = () => {
const user = auth.currentUser;
if (user && user.providerData && user.providerData.length > 0) {
const providerData = user.providerData[0];
if (providerData.providerId === "google.com") {
return providerData.displayName;
}
}
return null;
};
export {
loginWithGoogle,
logoutFromGoogle,
addActivity,
getActivities,
buyRoom,
auth
};