Firebase - adding additional user data to split the database

I am new to Firebase and use the email / password login method. I have a method that works fine, however in my registration form I have additional fields that I want to enter into the database. As far as I understand from the registration method by email / password, any additional fields should be stored separately from how authentication by email / password is stored during user registration.

I created a subsection in my databases to store additional user data called users. this is how it looks in firebase:

enter image description here

Here is the function I call (in React) to create a new user:

    signUp(e) {
        e.preventDefault();
        var firstName = $('.signup-first-name').val();
        var lastName = $('.signup-last-name').val();
        var userName = $('.signup-user-name').val();
        var password = $('.signup-user-password').val();
        var email = $('.signup-email').val();   

        var auth = firebase.auth();

        const promise = auth.createUserWithEmailAndPassword(email,password).then(function(user) {

        var user = firebase.auth().currentUser;
        user.updateProfile({
            displayName: userName,
        }).then(function() {
        // Update successful.

        // new db code here
        var ref = new firebase("https://music-app-7a4d3.firebaseio.com");

        ref.onAuth(function(authData) {
            ref.child("users").child(authData.uid).set({
                firstName: firstName,
                lastName: lastName
            })
        })
        // end new db code here

    }, function(error) {
        // An error happened.
    });  

}, function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
    if (errorCode == 'auth/weak-password') {
        alert('The password is too weak.');
    } else {
        console.error(error);
    }
});

        promise.catch(e => console.log(e.message));

        firebase.auth().onAuthStateChanged(firebaseUser => {
           if(firebaseUser) {
               console.log(firebaseUser)
           } else {
               console.log("not logged in")
           }
        });

    }

Another example that I found here followed:

Firebase

, . , !

+7
2
+8

.

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

Source: https://habr.com/ru/post/1659071/


All Articles