23

Firebase v3 Reference Guide indicates that createUserWithEmailAndPassword(email, password) returns firebase.Promise containing non-null firebase.User.

The Firebase v3 Authenticate with Firebase using Password-Based Accounts Guide shows the following example

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

My question is, I want to fire a function which adds the user to my users node in my Realtime Database. I want to include the users display name that I collected from the sites registration form. So, it seems like I want to do a .then and if the createUserWithEmailAndPassword method was successful I want to fire a function that writes the new user (with display name) to my Realtime Database users node.

How do I modify the example script to a .then style?

Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91

4 Answers4

44

Here is the correct use of then() and error handling with createUserWithEmailAndPassword:

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

function logUser(user) {
    var ref = firebase.database().ref("users");
    var obj = {
        "user": user,
        ...
    };
    ref.push(obj); // or however you wish to update the node
}

I have another example here.

Community
  • 1
  • 1
theblindprophet
  • 7,767
  • 5
  • 37
  • 55
4

Check this, you need to read payload or snapshot(firebase term)

firebase.auth().createUserWithEmailAndPassword(credentials.email, credentials.password)
                .then((authData) => {
                    console.log("User created successfully with payload-", authData);
                    //Write code to use authData to add to Users
                }).catch((_error) => {
                    console.log("Login Failed!", _error);
                })
Mithun Pattankar
  • 1,372
  • 1
  • 8
  • 12
3

Here's what I did and it worked.

        function registerUsername(email,password,displayName){
            firebase.auth().createUserWithEmailAndPassword(email, password).then(function(value) {
                console.log(value);
                }).catch(function(error) {
                    console.log(error);
                });
        }
Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91
  • @MithunPattankar I voted your answer up but accepted the other one just bcs it was easier for me to read. The ES6 you used is sharp but not as simple to me. Thank you very much for your answer though. – Ronnie Royston Jul 21 '16 at 20:36
1

Just for future stumbles upon it, an await-async example:

export const doCreateUserWithEmailAndPassword = async (pEmail, pPassword) => {
    try {
        const authResult = await app.auth().createUserWithEmailAndPassword(pEmail, pPassword);
         usersRef.doc(authResult.user.uid)
            .set({
                email: pEmail,
                created: firebase.firestore.FieldValue.serverTimestamp(),
              });
    }
    catch(error){
        console.log(error);
    }
}
realtimez
  • 2,525
  • 2
  • 20
  • 24