最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - save user's extra details on signup firebase - Stack Overflow

programmeradmin0浏览0评论

I want to save the user's extra details i.e number, age only when the user signup(one time). But there is no callback for to check if the signup was successful or not.

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

so what to do when you want to store the data only one time when the user signup rather using

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
  } else {
    // No user is signed in.
  }
});

which will be fired every time user login/logout but I dont want to save the extra signup details every time.

I want to save the user's extra details i.e number, age only when the user signup(one time). But there is no callback for to check if the signup was successful or not.

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

so what to do when you want to store the data only one time when the user signup rather using

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
  } else {
    // No user is signed in.
  }
});

which will be fired every time user login/logout but I dont want to save the extra signup details every time.

Share Improve this question asked Jun 29, 2016 at 13:48 StealthTrailsStealthTrails 2,4158 gold badges46 silver badges70 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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' bination 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);
        });

Here I have saved data in 'user.uid' key which is returned by firebase upon successful registration of user.

Calling createUserWithEmailAndPassword() return a promise, which has both a catch() and then() method you can respond to.

The Firebase documentation intentionally doesn't use then then() clause, since it's in general better to respond to the onAuthStateChanged() event (which also fires when the user reloads the app and is still signed in. Even in your case that might be the better place to put the code to store the user profile, as some of the data might change when the app reloads.

But if you want to explicitly respond to "the user was created successfully", you can do so with:

firebase.auth()
    .createUserWithEmailAndPassword(email, password)
    .then(function(user) {
        console.log("Create user and sign in Success", user);
    });
发布评论

评论列表(0)

  1. 暂无评论