I´m creating profiles for signed in users. The problem that I´m having is that under some circumstances is allowing to create duplicate profiles for a given uid instead of returning the one that already exists.
const unsubscribe = firebase.auth().onAuthStateChanged((user) => {
if (user) {
findProfile(user.uid)
.then((profile) => console.log(profile))
.catch((error) => createProfile(user.uid))
} else {
console.log("user not signed in")
}
});
export const findProfile = (uid) => {
return firebase
.collection("profiles")
.where("uid", "==", uid)
.get()
.then((querySnapshot) => {
if (querySnapshot.size > 0) {
return querySnapshot.docs[0];
} else {
throw new Error("profile not found");
}
});
};
export const createProfile = (uid) => {
return firebase.collection("profiles").add({uid});
};