How to add username with email and password in Firebase?

后端 未结 3 1609
野趣味
野趣味 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

    0 讨论(0)
  • 2021-02-05 16:16

    You need to create that database users child node yourself ;-)

    The createUserWithEmailAndPassword function only creates a new user in Firebase authentication service. The database itself isn't changed at all as a result.

    To add this new user to the database as well, try:

    firebase.database().ref("users").child(user.uid).set(...)
    
    0 讨论(0)
  • 2021-02-05 16:37

    You can create a users endpoint and store custom user data in there.

    function writeUserData(userId, name, email, imageUrl) {
      firebase.database().ref('users/' + userId).set({
        username: name,
        email: email,
        profile_picture : imageUrl,
        // Add more stuff here
      });
    }
    

    Have a look to https://firebase.google.com/docs/database/web/read-and-write

    0 讨论(0)
提交回复
热议问题