Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get UID of recently created user on Firebase

Is there a way to get the UID of a recently created user?

According to createUser() documentation, it doesn't look like it returns anything.

How would one go about obtaining this information so that we can start storing information about the user?

I know a way that could be achieved would be logging in the user upon creation. But I don't want to overwrite my existing session.

var firebaseRef = new Firebase('https://samplechat.firebaseio-demo.com');
  firebaseRef.createUser({
    email    : "[email protected]",
    password : "correcthorsebatterystaple"
  }, function(err) {
    if (err) {
      switch (err.code) {
        case 'EMAIL_TAKEN':
          // The new user account cannot be created because the email is already in use.
        case 'INVALID_EMAIL':
          // The specified email is not a valid email.
        case default:
      }
    } else {
      // User account created successfully!
    }
  });
like image 331
bryan Avatar asked Feb 11 '23 12:02

bryan


2 Answers

The above answers are for old firebase. For the ones looking for new firebase implementation :

     firebase.auth().createUserWithEmailAndPassword(email, password)
      .then(function success(userData){
          var uid = userData.uid; // The UID of recently created user on firebase

          var displayName = userData.displayName;
          var email = userData.email;
          var emailVerified = userData.emailVerified;
          var photoURL = userData.photoURL;
          var isAnonymous = userData.isAnonymous;
          var providerData = userData.providerData;

      }).catch(function failure(error) {

          var errorCode = error.code;
          var errorMessage = error.message;
          console.log(errorCode + " " + errorMessage);

      });

Source : Firebase Authentication Documentation

like image 76
reverie_ss Avatar answered May 16 '23 08:05

reverie_ss


Firebase recently released an updated JavaScript client (v2.0.5) which directly exposes the user id of the newly-created user via the second argument to the completion callback. Check out the changelog at https://www.firebase.com/docs/web/changelog.html and see below for an example:

ref.createUser({
  email: '...',
  password: '...'
}, function(err, user) {
  if (!err) {
    console.log('User created with id', user.uid);
  }
});
like image 23
Rob DiMarco Avatar answered May 16 '23 08:05

Rob DiMarco