From fb5a857592c190d48a5f3d5aacd907b5f1f5c317 Mon Sep 17 00:00:00 2001 From: solderq35 Date: Sun, 14 Jul 2024 06:17:06 +0200 Subject: [PATCH 01/15] email verify on signup poc --- app/segments/(team)/chooseTeam.js | 31 +++++++++++++++++++++++++++++-- context/Auth.js | 6 ++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/app/segments/(team)/chooseTeam.js b/app/segments/(team)/chooseTeam.js index 246ef913..15124931 100644 --- a/app/segments/(team)/chooseTeam.js +++ b/app/segments/(team)/chooseTeam.js @@ -1,6 +1,6 @@ import { useQueryClient } from "@tanstack/react-query"; import { router } from "expo-router"; -import { signOut as signoutFireBase } from "firebase/auth"; +import { sendEmailVerification } from "firebase/auth"; import { doc, getDoc, setDoc } from "firebase/firestore"; import { useEffect, useState } from "react"; import { Text, View } from "react-native"; @@ -16,6 +16,7 @@ import { invalidateMultipleKeys } from "~/dbOperations/invalidateMultipleKeys"; import { auth, db } from "~/firebaseConfig"; function ChooseTeam() { + // const auth = getAuth(); const { signOut, currentUserId, currentUserInfo, setCurrentUserId } = useAuthContext(); const queryClient = useQueryClient(); @@ -99,6 +100,7 @@ function ChooseTeam() { > + + )} - - )} - - - + + Sign Out + + + - + ); } From 1c5b90694e8df788f4b629ce261c410e6a8406c9 Mon Sep 17 00:00:00 2001 From: solderq35 Date: Sat, 3 Aug 2024 07:10:05 +0200 Subject: [PATCH 07/15] fix signout error --- app/content/profile/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/content/profile/index.js b/app/content/profile/index.js index b37aff02..44351af1 100644 --- a/app/content/profile/index.js +++ b/app/content/profile/index.js @@ -171,7 +171,7 @@ function Index() { newPassword, newPasswordCheck, passwordInputVisible, - userData.name, + userData, userEmail, ], ); From cfd9e15d593d706d333b2d34d7bb4e2c59f178bd Mon Sep 17 00:00:00 2001 From: Jake Gehrke Date: Sun, 4 Aug 2024 00:53:08 -0700 Subject: [PATCH 08/15] fix - added email to blacklist record --- app/content/team/users/[user]/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/content/team/users/[user]/index.js b/app/content/team/users/[user]/index.js index e9322460..f1488bcc 100644 --- a/app/content/team/users/[user]/index.js +++ b/app/content/team/users/[user]/index.js @@ -26,13 +26,14 @@ import { removeUser } from "~/dbOperations/removeUser"; import { db } from "~/firebaseConfig"; //A function to add a user to the blacklist table with a timestamp -async function blacklistUser(teamId, userId, userInfo) { +async function blacklistUser(teamId, userId, userInfo, userEmail) { //Create new document with userId as the id and a time field try { await setDoc(doc(db, "teams", teamId, "blacklist", userId), { time: Date.now(), name: userInfo["name"], + email: userEmail, }); //remove users data await removeUser(teamId, userId); @@ -336,7 +337,7 @@ function Index() { hideBanDialog, async () => { try { - await blacklistUser(currentTeamId, userId, userInfo); + await blacklistUser(currentTeamId, userId, userInfo, userEmail); await queryClient.removeQueries(["userInfo", userId]); await invalidateMultipleKeys(queryClient, [ ["userInfo"], From 6bff91c4436c727dc4ae01c6f8b376ec71ecc357 Mon Sep 17 00:00:00 2001 From: Jake Gehrke Date: Sun, 4 Aug 2024 00:55:19 -0700 Subject: [PATCH 09/15] fix - added assertion if pfp exists when removing a user --- dbOperations/removeUser.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/dbOperations/removeUser.js b/dbOperations/removeUser.js index d53d8e38..f167b072 100644 --- a/dbOperations/removeUser.js +++ b/dbOperations/removeUser.js @@ -7,9 +7,9 @@ import { runTransaction, where, } from "firebase/firestore"; -import removePfp from "~/dbOperations/removePfp"; -import { db } from "~/firebaseConfig"; import { getPfpName } from "~/Utility"; +import { db } from "~/firebaseConfig"; +import removePfp from "./removePfp"; async function removeUser(teamId, userId) { try { @@ -47,9 +47,14 @@ async function removeUser(teamId, userId) { //Remove user from user table where UID == userID const userRef = doc(db, "teams", teamId, "users", userId); - await transaction.delete(userRef); + const userSnapshot = await transaction.get(userRef); - await removePfp(getPfpName(teamId, userId)); + //remove pfp if there is one + if (userSnapshot.data().pfp !== "") { + await removePfp(getPfpName(teamId, userId)); + } + + await transaction.delete(userRef); console.log(" Remove User Transaction has completed"); }); From c05092d0489f3160c44f6f82de20280baaabff90 Mon Sep 17 00:00:00 2001 From: Frankreed Date: Mon, 5 Aug 2024 09:38:45 +0700 Subject: [PATCH 10/15] polished email verification flow --- app/segments/(team)/chooseTeam.js | 180 +++++++++++++----------------- 1 file changed, 77 insertions(+), 103 deletions(-) diff --git a/app/segments/(team)/chooseTeam.js b/app/segments/(team)/chooseTeam.js index 224a4985..678acd56 100644 --- a/app/segments/(team)/chooseTeam.js +++ b/app/segments/(team)/chooseTeam.js @@ -32,6 +32,8 @@ function ChooseTeam() { const [loading, setLoading] = useState(true); const [error, setError] = useState(null); + const [verified, setVerified] = useState(false); + async function handleSignOut() { try { await signoutFireBase(auth); @@ -63,6 +65,22 @@ function ChooseTeam() { } }, [currentUserId]); + useEffect(() => { + const unregisterAuthObserver = onIdTokenChanged(auth, async (user) => { + if (user) { + if (user.emailVerified) { + setVerified(true); + showSnackBar("Email successfully verified."); + unregisterAuthObserver(); + } else { + setVerified(false); + console.log("Error: Email Not Verified Yet, Try Again"); + } + } + }); + return unregisterAuthObserver; + }, [showSnackBar]); + const [refreshing, setRefreshing] = useState(false); const onRefresh = useCallback(() => { @@ -111,74 +129,65 @@ function ChooseTeam() { > You've been banned from this team. + ) : verified ? ( + ) : ( + Waiting for email verification... )} - - - - + Sign Out + + From e8d28d49303d9e4bf52568016a8e1a1173b110fd Mon Sep 17 00:00:00 2001 From: solderq35 Date: Wed, 7 Aug 2024 10:58:27 +0200 Subject: [PATCH 11/15] loading spinner kind of works for send email --- app/segments/(team)/chooseTeam.js | 17 +++++++++-------- dbOperations/hooks/useUserInfo.js | 14 +++++++++++--- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/app/segments/(team)/chooseTeam.js b/app/segments/(team)/chooseTeam.js index 678acd56..6e721321 100644 --- a/app/segments/(team)/chooseTeam.js +++ b/app/segments/(team)/chooseTeam.js @@ -13,14 +13,12 @@ import { SafeAreaView } from "react-native-safe-area-context"; import { themeColors } from "~/Constants"; import { getErrorString } from "~/Utility"; import ErrorComponent from "~/components/errorComponent"; -import Loading from "~/components/loading"; import { useAlertContext } from "~/context/Alert"; import { useAuthContext } from "~/context/Auth"; import { invalidateMultipleKeys } from "~/dbOperations/invalidateMultipleKeys"; import { auth, db } from "~/firebaseConfig"; function ChooseTeam() { - // const auth = getAuth(); const { signOut, currentUserId, currentUserInfo, setCurrentUserId } = useAuthContext(); const queryClient = useQueryClient(); @@ -68,6 +66,10 @@ function ChooseTeam() { useEffect(() => { const unregisterAuthObserver = onIdTokenChanged(auth, async (user) => { if (user) { + await auth.currentUser.reload(); + console.log("USER RELOADED AFTER VERIFICATION"); + console.log(user.emailVerified); + console.log(auth.currentUser.emailVerified); if (user.emailVerified) { setVerified(true); showSnackBar("Email successfully verified."); @@ -79,7 +81,7 @@ function ChooseTeam() { } }); return unregisterAuthObserver; - }, [showSnackBar]); + }, [auth.currentUser]); const [refreshing, setRefreshing] = useState(false); @@ -92,10 +94,6 @@ function ChooseTeam() { }, 2000); }, []); - if (loading) { - return ; - } - if (error) { return ; } @@ -179,15 +177,18 @@ function ChooseTeam() { marginTop: 20, }} onPress={async () => { + setLoading(true); try { await sendEmailVerification(auth.currentUser); console.log("Verification Email Sent!"); showSnackBar("Verification Email Sent!"); - } catch { + } catch (e) { console.log("Error sending verification email: ", e); showDialog("Error", getErrorString(e)); } + setLoading(false); }} + loading={loading} > { - const { currentTeamId, currentUserId } = useAuthContext(); + const { currentTeamId, currentUserId, currentUserVerified } = + useAuthContext(); const week_milliseconds = 604800000; const currentDate = new Date(); const currentDateTime = currentDate.getTime(); @@ -31,7 +32,14 @@ export const useUserInfo = ({ doc(db, "teams", currentTeamId, "users", userId), ); const data = querySnapshot.data(); - if (!data) { + + // DEBUG REMOVE BELOW LOG LINES LATER + console.log("DATA"); + console.log(data); + console.log("VERIFIED STATUS"); + console.log(auth.currentUser.emailVerified); + + if (!data || !auth.currentUser.emailVerified) { if (currentUserId === userId) { router.replace("segments/(team)/chooseTeam"); } From 71323adfc488741a95e330c1764a471ff9a7cd7b Mon Sep 17 00:00:00 2001 From: solderq35 Date: Thu, 8 Aug 2024 06:39:23 +0200 Subject: [PATCH 12/15] spinner color + useEffect dependency --- app/segments/(team)/chooseTeam.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/app/segments/(team)/chooseTeam.js b/app/segments/(team)/chooseTeam.js index 6e721321..9c311aaa 100644 --- a/app/segments/(team)/chooseTeam.js +++ b/app/segments/(team)/chooseTeam.js @@ -66,10 +66,6 @@ function ChooseTeam() { useEffect(() => { const unregisterAuthObserver = onIdTokenChanged(auth, async (user) => { if (user) { - await auth.currentUser.reload(); - console.log("USER RELOADED AFTER VERIFICATION"); - console.log(user.emailVerified); - console.log(auth.currentUser.emailVerified); if (user.emailVerified) { setVerified(true); showSnackBar("Email successfully verified."); @@ -81,7 +77,7 @@ function ChooseTeam() { } }); return unregisterAuthObserver; - }, [auth.currentUser]); + }, []); const [refreshing, setRefreshing] = useState(false); @@ -189,6 +185,7 @@ function ChooseTeam() { setLoading(false); }} loading={loading} + textColor="white" > Date: Fri, 9 Aug 2024 05:37:26 +0200 Subject: [PATCH 13/15] useEffect verify status checker works --- app/segments/(team)/chooseTeam.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/app/segments/(team)/chooseTeam.js b/app/segments/(team)/chooseTeam.js index 9c311aaa..41e8bfa9 100644 --- a/app/segments/(team)/chooseTeam.js +++ b/app/segments/(team)/chooseTeam.js @@ -69,14 +69,26 @@ function ChooseTeam() { if (user.emailVerified) { setVerified(true); showSnackBar("Email successfully verified."); - unregisterAuthObserver(); + clearInterval(intervalId); // Stop the interval when email is verified + unregisterAuthObserver(); // Unregister the auth observer } else { setVerified(false); console.log("Error: Email Not Verified Yet, Try Again"); } } }); - return unregisterAuthObserver; + + // Set up an interval to check email verification every 10 seconds + const intervalId = setInterval(async () => { + if (auth.currentUser) { + await auth.currentUser.reload(); + } + }, 10000); // 10,000 ms = 10 seconds + + return () => { + clearInterval(intervalId); // Clean up the interval when component unmounts + unregisterAuthObserver(); // Unregister the auth observer + }; }, []); const [refreshing, setRefreshing] = useState(false); From 69b6322cfc0d8564b927f921b64c41ba3a58fe00 Mon Sep 17 00:00:00 2001 From: solderq35 Date: Sat, 10 Aug 2024 23:18:20 +0200 Subject: [PATCH 14/15] fix setCurrentUserVerified and clean up logs --- app/segments/(team)/chooseTeam.js | 1 - context/Auth.js | 6 +++--- dbOperations/hooks/useUserInfo.js | 10 ++-------- 3 files changed, 5 insertions(+), 12 deletions(-) diff --git a/app/segments/(team)/chooseTeam.js b/app/segments/(team)/chooseTeam.js index 41e8bfa9..5335d603 100644 --- a/app/segments/(team)/chooseTeam.js +++ b/app/segments/(team)/chooseTeam.js @@ -97,7 +97,6 @@ function ChooseTeam() { setRefreshing(true); setTimeout(async () => { await auth.currentUser.reload(); - console.log("user reloaded"); setRefreshing(false); }, 2000); }, []); diff --git a/context/Auth.js b/context/Auth.js index 7e672331..e73d4f4f 100644 --- a/context/Auth.js +++ b/context/Auth.js @@ -1,5 +1,5 @@ import { useRouter, useSegments } from "expo-router"; -import { onAuthStateChanged } from "firebase/auth"; +import { onIdTokenChanged } from "firebase/auth"; import { createContext, useContext, useEffect, useState } from "react"; import { auth } from "~/firebaseConfig"; @@ -42,7 +42,7 @@ export const AuthProvider = ({ children }) => { useEffect(() => { //if this code is not in here, it'll run for infinite times - onAuthStateChanged(auth, (newlyLoggedInUser) => { + onIdTokenChanged(auth, (newlyLoggedInUser) => { // test user login (yarn test) // If you sign out, reload or click "sign in" to login as test user // Signout functionality for test user is buggy, chance of auto-logging back in @@ -63,7 +63,7 @@ export const AuthProvider = ({ children }) => { } } }); - }, []); + }, [currentUserVerified]); return ( Date: Sat, 10 Aug 2024 23:33:03 +0200 Subject: [PATCH 15/15] remove settimeout from chooseteam pulldown refresh --- app/segments/(team)/chooseTeam.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/app/segments/(team)/chooseTeam.js b/app/segments/(team)/chooseTeam.js index 5335d603..0448e3f1 100644 --- a/app/segments/(team)/chooseTeam.js +++ b/app/segments/(team)/chooseTeam.js @@ -93,12 +93,10 @@ function ChooseTeam() { const [refreshing, setRefreshing] = useState(false); - const onRefresh = useCallback(() => { + const onRefresh = useCallback(async () => { setRefreshing(true); - setTimeout(async () => { - await auth.currentUser.reload(); - setRefreshing(false); - }, 2000); + await auth.currentUser.reload(); + setRefreshing(false); }, []); if (error) {