1

I want to have my create users to be added into my database by their user uid when i sign them up from my firebase.

currently my code is working , just i dont understand why when my data is save into my firebase, the name (which is suppose to be the created user uid) shows "undefined". but the data that is grabbed and saved is correct.

My firebase database which shows undefined: https://i.stack.imgur.com/KVFBT.jpg

My JS code which i am trying to save and create user:

/*Show Login User*/

// Firebase Variables
var auth = firebase.auth();

$(document).ready(function () {

  //Pass parameter from form to Firebase
  $('.addpic').on('submit', event => {
    event.preventDefault();
    const name = $('#name').val();
    const hp = $('#hp').val();
    const email = $('#email').val();
    const password = $('#password').val();
    const role = $('#role').val();

    //Firebase user authentication
    firebase.auth().createUserWithEmailAndPassword(email, password)
      .then(user => {

        //set data into User database
        firebase.database().ref('Admin/Person In Charge' + "/" + user.uid).set({
          Name: name,
          ContactNo: hp,
          Email: email,
          Password: password,
          Role: role
        }).then(success => {
          console.log(user);
          window.alert("Registered");
          window.location = "user.html";
        });
      })
      .catch(function (error) {

        var errorCode = error.code;
        var errorMessage = error.message;

        window.alert("Error : " + errorMessage);
      });
  });
});

I want my data to be saved under the UID of the created user. I tried all possible solutions, but none work for me.

imjared
  • 19,492
  • 4
  • 49
  • 72
justarandomguy
  • 145
  • 1
  • 13

1 Answers1

2

You should take a look at the return type of createUserWithEmailAndPassword. If I'm reading the docs correctly, it returns an instance of firebase.auth.UserCredential, not an actual user. I think you need to actually drill down one more level into that credential object and get the user.uid.

Example

firebase.auth().createUserWithEmailAndPassword(email, password)
  .then(userCredential => {

    //set data into User database
    firebase.database().ref('Admin/Person In Charge' + "/" + userCredential.user.uid).set({
      Name: name,
      ContactNo: hp,
      Email: email,
      Role: role
    }).then(success => {
      console.log(user);
      window.alert("Registered");
      window.location = "user.html";
    });
  })

You could figure this out in the future by inspecting the value of your user in your then via a console.log().

imjared
  • 19,492
  • 4
  • 49
  • 72
  • First of all, thanks for the time to check on my enormous code block and edited all the unused code out! I tried this answer, and it works flawlessly on my code! Thank you for you kind help!! – justarandomguy Feb 04 '19 at 02:56
  • But here the password will save as a plaintext ! – Oliver D Nov 25 '19 at 12:47
  • @OliverD good point. i had just copy+pasted from OP. i've now removed the password line. – imjared Nov 25 '19 at 13:20
  • Great :D but if i want to save them into firebase for a case if users want to change his password how can i handle it ? – Oliver D Nov 25 '19 at 13:45
  • @imjared AND can you check this question please, https://stackoverflow.com/questions/59033989/how-to-use-multi-auth-firebase – Oliver D Nov 25 '19 at 14:51