this is my Register function
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
const uid = userCredential.user.uid;
const data = {
id: uid,
email,
fullName,
};
async function storeUserUid() {
const newUser = doc(collection(db, "users", user.uid));
await setDoc(newUser, data);
console.log(uid) = aj3x5gAe2jUcngBoTY5cVpOTITu1
console.log(data) = Object {
"email": "lala@email.com",
"fullName": "lala",
"id": "aj3x5gAe2jUcngBoTY5cVpOTITu1",
}
My login function that needs helping. When authenticating it successfully I have console.logged the (uid) which is the first markup in the screenshow below. I am receiving the same uid from the second console.log(userList) under the "id" key. How can I access the object and compare both values so I can let the user then navigate to the "HomeScreen" if there is a match from the signInwithCredentials and the userList id. I need somehow to map through all the docs inside the "users" collection and compare their "id" keys and if there is a match with the signInwithCredentials uid => then, let the user in. MANY THANKS
const onLoginPress = () => {
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
const uid = userCredential.user.uid;
console.log(uid);
async function getUser() {
const q = query(collection(db, "users"));
const userSnap = await getDocs(q);
const userList = userSnap.docs.map((doc) => doc.data());
console.log(userList);
}
getUser();
})
If you don't need to get all user docs, but just the one for the current user, that'd be:
async function getUser() {
const ref = doc(db, "users", FirebaseAuth.instance.currentUser.uid));
const userDoc = await getDoc(ref);
return userDoc.data();
}
getUser();