-1

I am using firebase.auth().createUserWithEmailAndPassword but I also wanted to save additional data such as the first name and last name in the registration screen.

I have tried doing this code but it only creates the user with the email and password. It does not save the first name and last name of the user in the database.

signUpUser = (email, password, firstName, lastName) => {
    try {
      if (this.state.password.length < 6) {
        alert("Please enter atleast 6 characters");
        return;
      }
      firebase.auth().createUserWithEmailAndPassword(email,password)
      .then(
        firebase.database().ref('users.' + firebase.auth().currentUser.uid + '/').set({
          firstName: firstName,
          lastName: lastName
        })
      )

    } catch (error) {
      console.log(error.toString());
    }
  }
1
  • 1
    So you're passing the result of the set() function to then()? then() normally takes a function argument for a callback. set() returns a promise, which you should instead check for error. Jan 19, 2019 at 2:19

1 Answer 1

0

export const addProfile = ({firstname, lastname, email}) => new Promise((resolve, reject) => {

  var d = new Date();
  var userId = firebaseApp.auth().currentUser.uid;

  var userProfile = {
    created: d,
    firstname: firstname,
    lastname: lastname,
  };

  firebaseApp.database().ref(`profiles/${userId}`).set(userProfile)
  .then( () => resolve(userProfile) )
  .catch( error => reject(error) );

});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.