Firebase v3 createUserWithEmailAndPassword .then Promise

后端 未结 4 1923
醉话见心
醉话见心 2020-12-09 08:56

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

相关标签:
4条回答
  • 2020-12-09 09:48

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-09 09:55

    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);
                    });
            }
    
    0 讨论(0)
  • 2020-12-09 09:59

    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.

    0 讨论(0)
  • 2020-12-09 09:59

    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);
                    })
    
    0 讨论(0)
提交回复
热议问题