简体   繁体   中英

How to Retrieve User UID After They Have Been Created Firebase JS

I am trying to retrieve the UID of a user I just created in Firebase using JavaScript. Below is my current code:

firebase.auth().createUserWithEmailAndPassword(email, pass).catch(function(error, data) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  newusercreateduuid = data.user.uid;
  // ...
  console.log(errorCode + '   Error Message: ' + errorMessage);
});

I have tried a variety of callbacks including userData, data, user, and many more, but they all return null. I cannot seem to find anything online. I did find another Stack Overflow post using userData, but that returned null for me. How can I retrieve the UID of the user I just created?

you need to add this:

You can access to the then method:

firebase.auth()
.createUserWithEmailAndPassword(email, password)
.then(function(user) 
    // user information is available here...

    
})
.catch(function(error, data) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  newusercreateduuid = data.user.uid;
  // ...
  console.log(errorCode + '   Error Message: ' + errorMessage);
});

Also, if you need to control the user log changes, you can do this:

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
    var displayName = user.displayName;
    var email = user.email;
    var emailVerified = user.emailVerified;
    var photoURL = user.photoURL;
    var isAnonymous = user.isAnonymous;
    var uid = user.uid;
    var providerData = user.providerData;
    // ...
  } else {
    // User is signed out.
    // ...
  }
});

This will give you the user state after login. Source

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