Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user doc ID doesn't match the firebase auth users UID - React Native Expo #1

Open
ryanbakker opened this issue Dec 15, 2023 · 0 comments
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed

Comments

@ryanbakker
Copy link
Owner

When a new user is sent to the firestore database, I want the doc ID of the user to match the users UID in firebase auth. Currently the document is being assigned a random ID. The user's data is being sent to the db in the following useEffect:

React.useEffect(() => {
    checkLocalUser();
    const unsub = onAuthStateChanged(auth, async (user) => {
      if (user) {
        await AsyncStorage.setItem("@user", JSON.stringify(user));
        setUserInfo(user);

        // Check if the user already exists in Firestore
        const usersRef = collection(db, "users");
        const q = query(usersRef, where("userId", "==", user.uid));
        const querySnapshot = await getDocs(q);

        if (querySnapshot.size === 0) {
          // User does not exist, add them to Firestore
          await addDoc(usersRef, {
            displayName: user.displayName,
            photoURL: user.photoURL,
            email: user.email,
            userId: user.uid,
            coverPhoto: "",
            bio: "",
          });

          console.log("New User => ", user.displayName, user.uid);
        } else {
          console.log("User already exists:", user.displayName, user.uid);
        }
      } else {
        console.log("User not authenticated");
      }
    });

    return () => unsub();
  }, []);
@ryanbakker ryanbakker added bug Something isn't working help wanted Extra attention is needed good first issue Good for newcomers labels Dec 15, 2023
@ryanbakker ryanbakker pinned this issue Dec 15, 2023
@ryanbakker ryanbakker changed the title user doc ID doesn't match the firebase auth users UID user doc ID doesn't match the firebase auth users UID - React Native Expo Dec 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant