使用React Native在Firebase中保存其他注册用户数据

时间:2019-01-19 02:12:30

标签: firebase react-native firebase-realtime-database firebase-authentication

我正在使用firebase.auth()。createUserWithEmailAndPassword,但我也想在注册屏幕中保存其他数据,例如名字和姓氏。

我尝试执行此代码,但它仅使用电子邮件和密码创建用户。它不会在数据库中保存用户的名字和姓氏。

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 个答案:

答案 0 :(得分: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) );

});