From b0f585b7997320d79acc632461d9997a283b9d46 Mon Sep 17 00:00:00 2001 From: Frank Nguyen <41023671+FrankreedX@users.noreply.github.com> Date: Thu, 15 Aug 2024 19:57:17 +0700 Subject: [PATCH 1/2] handle newly logged in user not having perms (#316) --- context/Alert.js | 2 ++ dbOperations/hooks/useUserInfo.js | 19 +++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/context/Alert.js b/context/Alert.js index 15ee2beb..05fd3738 100644 --- a/context/Alert.js +++ b/context/Alert.js @@ -17,10 +17,12 @@ export const AlertProvider = ({ children }) => { const [snackbarVisible, setSnackbarVisible] = useState(false); const [snackbarMessage, setSnackbarMessage] = useState(""); + return ( { + if (title === "Error" && message === "permission-denied") return; //for initial sign in. do this or change the rules setDialogTitle(title); setDialogMessage(message); setDialogVisible(true); diff --git a/dbOperations/hooks/useUserInfo.js b/dbOperations/hooks/useUserInfo.js index a6f9c96c..6e626e82 100644 --- a/dbOperations/hooks/useUserInfo.js +++ b/dbOperations/hooks/useUserInfo.js @@ -11,6 +11,7 @@ import { } from "firebase/firestore"; import { useAuthContext } from "~/context/Auth"; import { db } from "~/firebaseConfig"; +import { getErrorString } from "~/Utility"; export const useUserInfo = ({ userId = null, @@ -28,10 +29,20 @@ export const useUserInfo = ({ queryFn: async () => { console.log("fetching userInfo: ", { userId, role }); if (userId) { - const querySnapshot = await getDoc( - doc(db, "teams", currentTeamId, "users", userId), - ); - const data = querySnapshot.data(); + let data; + try { + const querySnapshot = await getDoc( + doc(db, "teams", currentTeamId, "users", userId), + ); + data = querySnapshot.data(); + } catch (e) { + if (getErrorString(e) === "permission-denied") { + data = undefined; + } else { + throw e; + } + console.log("e", getErrorString(e)); + } if (!data || !currentUserVerified) { if (currentUserId === userId) { From f1668ffb2aee0116b8f855586c1029cfed4187c1 Mon Sep 17 00:00:00 2001 From: Frank Nguyen <41023671+FrankreedX@users.noreply.github.com> Date: Thu, 15 Aug 2024 19:57:30 +0700 Subject: [PATCH 2/2] Added TESTING constant that bypass oregonstate.edu and verified requirements (#315) --- Constants.js | 11 ++++++++++- app/(auth)/signup.js | 4 ++-- app/segments/(team)/chooseTeam.js | 6 +++++- context/Auth.js | 6 +++++- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Constants.js b/Constants.js index d45112b3..6a9f7c96 100644 --- a/Constants.js +++ b/Constants.js @@ -7,6 +7,8 @@ const themeColors = { highlight: "#fff", }; +const TESTING = true; + const prettyTitle = { target: "Target", sideLanding: "Side Landing", @@ -55,4 +57,11 @@ const firebaseErrors = { "auth/missing-email": "Missing email", "auth/missing-password": "Missing password", }; -export { firebaseErrors, prettyRole, prettyTitle, shortTitle, themeColors }; +export { + TESTING, + firebaseErrors, + prettyRole, + prettyTitle, + shortTitle, + themeColors, +}; diff --git a/app/(auth)/signup.js b/app/(auth)/signup.js index df9ce866..cd38b0e8 100644 --- a/app/(auth)/signup.js +++ b/app/(auth)/signup.js @@ -18,7 +18,7 @@ import { } from "react-native"; import { KeyboardAwareScrollView } from "react-native-keyboard-aware-scroll-view"; import { Button } from "react-native-paper"; -import { themeColors } from "~/Constants"; +import { TESTING, themeColors } from "~/Constants"; import { getErrorString } from "~/Utility"; import ProfilePicture from "~/components/ProfilePicture"; import { useAlertContext } from "~/context/Alert"; @@ -45,7 +45,7 @@ export default function SignUp() { if (password !== passwordCheck) { throw "Passwords don't match"; } - if (!email.endsWith("@oregonstate.edu")) { + if (!TESTING && !email.endsWith("@oregonstate.edu")) { throw "Only @oregonstate.edu emails are allowed at this time."; } const userCredential = await createUserWithEmailAndPassword( diff --git a/app/segments/(team)/chooseTeam.js b/app/segments/(team)/chooseTeam.js index 0448e3f1..7b49e0fa 100644 --- a/app/segments/(team)/chooseTeam.js +++ b/app/segments/(team)/chooseTeam.js @@ -10,7 +10,7 @@ import { useCallback, useEffect, useState } from "react"; import { RefreshControl, ScrollView, Text, View } from "react-native"; import { Button } from "react-native-paper"; import { SafeAreaView } from "react-native-safe-area-context"; -import { themeColors } from "~/Constants"; +import { TESTING, themeColors } from "~/Constants"; import { getErrorString } from "~/Utility"; import ErrorComponent from "~/components/errorComponent"; import { useAlertContext } from "~/context/Alert"; @@ -64,6 +64,10 @@ function ChooseTeam() { }, [currentUserId]); useEffect(() => { + if (TESTING) { + setVerified(true); + return; + } const unregisterAuthObserver = onIdTokenChanged(auth, async (user) => { if (user) { if (user.emailVerified) { diff --git a/context/Auth.js b/context/Auth.js index e73d4f4f..ad7168ea 100644 --- a/context/Auth.js +++ b/context/Auth.js @@ -1,6 +1,7 @@ import { useRouter, useSegments } from "expo-router"; import { onIdTokenChanged } from "firebase/auth"; import { createContext, useContext, useEffect, useState } from "react"; +import { TESTING } from "~/Constants"; import { auth } from "~/firebaseConfig"; const AuthContext = createContext({ @@ -57,8 +58,11 @@ export const AuthProvider = ({ children }) => { setCurrentUserId(newlyLoggedInUser["uid"] ?? "Error (uid)"); setCurrentUserInfo(newlyLoggedInUser ?? {}); console.log("user changed. userId:", newlyLoggedInUser["uid"]); - if (auth.currentUser.emailVerified) { + + if (TESTING) { setCurrentUserVerified(true); + } else { + setCurrentUserVerified(auth.currentUser.emailVerified); } } }