Skip to content

Commit

Permalink
Added utils
Browse files Browse the repository at this point in the history
  • Loading branch information
cqb13 committed Sep 12, 2023
1 parent 43df831 commit 74c2a74
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
Empty file added components/misc/signUpPopup.tsx
Empty file.
23 changes: 23 additions & 0 deletions utils/firebase/account/deleteAccount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { collection, doc, getDocs, deleteDoc, query } from "firebase/firestore";
import googleSignOut from "./googleSignOut";
import { db } from "@lib/firebase";

export default async function deleteAccount(user: any) {
const users = collection(db, "users");
const userRef = doc(users, user.uid);
const scoreCollection = collection(users, user.uid, "scores");
let ids: any = [];
const scoreQuery = query(scoreCollection);

const scoreQuerySnapshot = await getDocs(scoreQuery);
scoreQuerySnapshot.forEach((doc) => {
ids.push(doc.id);
});

ids.forEach(async (id: any) => {
await deleteDoc(doc(scoreCollection, id));
});

deleteDoc(userRef);
googleSignOut();
}
23 changes: 23 additions & 0 deletions utils/firebase/account/googleSignIn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { collection, getDoc, setDoc, doc } from "firebase/firestore";
import { GoogleAuthProvider, signInWithPopup } from "firebase/auth";
import { auth, db } from "@lib/firebase";

const users = collection(db, "users");

export default async function googleSignIn() {
const provider = new GoogleAuthProvider();
const result = await signInWithPopup(auth, provider);
const user = result.user;
const userRef = doc(users, user.uid);
const userDoc = await getDoc(userRef);
if (!userDoc.exists()) {
await setDoc(userRef, {
displayName: user.displayName,
photoURL: user.photoURL,
email: user.email,
allScores: [],
highScore: 0,
lowScore: 0
});
}
}
6 changes: 6 additions & 0 deletions utils/firebase/account/googleSignOut.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { signOut } from "firebase/auth";
import { auth } from "@lib/firebase";

export default function googleSignOut() {
signOut(auth);
}
9 changes: 9 additions & 0 deletions utils/firebase/db/getUserDoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { collection, doc, getDoc } from "firebase/firestore";
import { db } from "@lib/firebase";

export default async function getUserDoc(user: any) {
const users = collection(db, "users");
const userRef = doc(users, user.uid);
const docSnap = await getDoc(userRef);
return docSnap.data();
}

0 comments on commit 74c2a74

Please sign in to comment.