保存用户在注册firebase上的额外细节

Cha*_*qas 2 javascript firebase-authentication firebase-realtime-database

我想保存用户的额外细节,即数字,年龄仅在用户注册时(一次).但是没有回调来检查注册是否成功.

firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // ...
});
Run Code Online (Sandbox Code Playgroud)

所以当你想在用户注册而不是使用时只存储一次数据时该怎么办

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
  } else {
    // No user is signed in.
  }
});
Run Code Online (Sandbox Code Playgroud)

每次用户登录/注销时都会触发,但我不想每次都保存额外的注册详细信息.

Bha*_*bra 7

firebase.auth().createUserWithEmailAndPassword($scope.email, $scope.password)
        .then(function(user) {
            var ref = firebase.database().ref().child("user");
            var data = {
                email: $scope.email,
                password: $scope.password,
                firstName: $scope.firstName,
                lastName: $scope.lastName,
                id:user.uid
            }
            ref.child(user.uid).set(data).then(function(ref) {//use 'child' and 'set' combination to save data in your own generated key
                console.log("Saved");
                $location.path('/profile');
            }, function(error) {
                console.log(error); 
            });
        })
        .catch(function(error) {
            var errorCode = error.code;
            var errorMessage = error.message;
            if (errorCode == 'auth/weak-password') {
                alert('The password is too weak.');
            } else if (errorCode == 'auth/email-already-in-use') {
                alert('The email is already taken.');
            } else if (errorCode == 'auth/weak-password') {
                alert('Password is weak');
            } else {
                alert(errorMessage);
            }
            console.log(error);
        });
Run Code Online (Sandbox Code Playgroud)

这是结果 这里我保存了'user.uid'键中的数据,该键在成功注册用户后由firebase返回.