Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding user information to firebase (signup) react native

createUserWithEmailAndPassword(auth, state.email, state.password)
        .then((userCredential) => {
          setDoc(doc(db, "users", userCredential.user.uid), {
            firstName: state.firstName,
            lastName: state.lastName,
            email: state.email,
            address: state.address,
            username: state.firstName,
          })

Hi, im trying to add username in firebase user collection. Username should be firstname.lastname but I dont know how should I type that. This is currently working perfect but I just want add in username field "Lastname" but dont know how

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You are on the right track. You just need to join the first and last names together.

You can use + as strings can be added or use string interpolation (which is more elegant).

// adding strings 
const username = state.firstName + '.' + state.lastName;

// using string interpolation.
// here, you use the backtick character (`), it is usually found below escape.
// then use ${variable} to interpolate variable.
const username = `${state.firstName}.${state.lastName}`;

So your final code could look like the following. I formatted it and used async/await (because setDoc returns a promise). I also made the code more elegant with object destructuring.

createUserWithEmailAndPassword(auth, state.email, state.password) 
 .then(async (userCredential) => {
    const { address, email, firstName, lastName } = state;
    const username = `${firstName}.${lastName}`;
    await setDoc(
      doc(db, 'users', userCredential.user.uid), 
      { address, email, firstName, lastName, username },
    );
 });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading