How to add username with email and password in Firebase?

后端 未结 3 1608
野趣味
野趣味 2021-02-05 15:51

I\'m using Firebase and I\'m trying to add a username to the database with the email and password.

\"This

3条回答
  •  孤街浪徒
    2021-02-05 16:15

    This cannot be done through createUserWithEmailAndPassword() but there is a firebase method for this . You will need to listen for when authentication state is changed , get the user , then update the profile info . See Below

    This code would come after createUserWithEmailAndPassword()

     firebase.auth().onAuthStateChanged(function(user) {
    
                    if (user) {
    
                       // Updates the user attributes:
    
                      user.updateProfile({ // <-- Update Method here
    
                        displayName: "NEW USER NAME",
                        photoURL: "https://example.com/jane-q-user/profile.jpg"
    
                      }).then(function() {
    
                        // Profile updated successfully!
                        //  "NEW USER NAME"
    
                        var displayName = user.displayName;
                        // "https://example.com/jane-q-user/profile.jpg"
                        var photoURL = user.photoURL;
    
                      }, function(error) {
                        // An error happened.
                      });     
    
                    }
        });
    

    As stated in firebase User Api here : https://firebase.google.com/docs/reference/js/firebase.User#updateProfile

    Hope this helps

提交回复
热议问题