简体   繁体   中英

How to get the uId for the user just created

So if I create a new user, how can I get it's uId and store it in a variable? For example the following, I create a user, and I need the newly created uId of it so I can create object inside the database with the same key as the uId.

firebase.auth().createUserWithEmailAndPassword(email, "test123")
    .then(function(user) {
    })
    .catch(function(error) {
      // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;
      // ...
    });

    firebase.auth().onAuthStateChanged(firebaseUser =>{
      var user = firebase.auth().currentUser;
      key = firebase.database().ref().child('users/' + user.uid);
    });

    startDate = startDate.toString();
    firebase.database().ref('Users/' + key).set({
      employeeId: key,
      Email: email,
      name: name,
      startDate: startDate,
      fcmToken: "",
      Availability: avi,
      Privilege: p
    });

Add real time listener after the user creation like:

firebase.auth().createUserWithEmailAndPassword(email, "test123")
    .then(function(user) {
    })
    .catch(function(error) {
      // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;
      // ...
    });

    firebase.auth().onAuthStateChanged(firebaseUser =>{

     var ref = firebase.database().ref().child('users/' + firebaseUser.uid);
     //set something
     ref.set({/*save something to a specific uid*/});

    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM