简体   繁体   中英

react native firebase authentication users id

I made an authentication using firebase and firestore. When the user registers, I create a collection called users in firestore and save the user's information in the document. My goal is to reach the document id in the users collection of the registered user. my codes:

let createUser = async (email, password, firstName, lastName) => {
    await firebase.auth().createUserWithEmailAndPassword(email, password)
        .then(() => {
            firebase.auth().currentUser.sendEmailVerification({
                handleCodeInApp: true,
                url: "https://hpom-ca649.firebaseapp.com",
            }).then(() => { alert({ alertVerification })
                }).catch((error) => { alert(error.message) })
                .then(() => {
                     addDoc(collection(db,'users'), {
                          first: {firstName},
                          last: {lastName},
                          email: {email}
                        })
                }).catch((error) => { alert(error.message);console.log(error.message) })
        })
}

在此处输入图像描述

If you want to be able to easily reach the user's document based on that user's ID, its best to store that document with the UID of the user as its document ID. You can do that by calling setDocument on a DocumentReference instead of addDocument on the CollectionReference .

In code:

firebase.auth().createUserWithEmailAndPassword(email, password)
    .then((credentials) => {
        setDoc(doc(db,'users', credentials.user.uid), {
          first: {firstName},
          last: {lastName},
          email: {email}
        })
    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM