Get UID of newly created user in Firebase

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

According to the createUser () documentation it looks like it doesn't return anything.

How can we get this information so that we can start storing user information?

I know that the path that can be reached will be logged by the user when created. But I don't want to overwrite the existing session.

var firebaseRef = new Firebase('https://samplechat.firebaseio-demo.com');
  firebaseRef.createUser({
    email    : "bobtony@firebase.com",
    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!
    }
  });

      

+3


source to share


4 answers


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



ref.createUser({
  email: '...',
  password: '...'
}, function(err, user) {
  if (!err) {
    console.log('User created with id', user.uid);
  }
});

      

+2


source


After creating a user, you can authenticate it as above, above the sample code on the page you are linking to:

Creates a new account based on email and password using the specified credentials. After creating an account, users can be authenticated using authWithPassword ().



then in the callback authWithPassword

you can access the new user auhtData

. https://www.firebase.com/docs/web/api/firebase/authwithpassword.html

+1


source


The above answers are for the old base. For anyone looking for a 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

+1


source


I asked this question on the firebase support forums and got this answer from Jacob. Hope this helps anyone with the same problem.

Copy and paste from http://groups.google.com/group/firebase-talk/


All you have to do is just authenticate with a different Firebase context. This can be done using an undocumented context argument when creating a new Firebase object.

// adminRef will be used to authenticate as you admin user (note the "admin" context - also note that this can be ANY string)
var adminRef = new Firebase("https://<your-firebase>.firebaseio.com", "admin");
adminRef.authWithCustomToken("<token>", function(error, authData) {
  if (error !== null) {  
    // now you are "logged in" as an admin user

    // Let create our user using our authenticated admin ref
    adminRef.createUser({
      email: <email>,
      password: <password>
    }, function(error) {
      if (error !== null) {
        // let create a new Firebase ref with a different context ("createUser" context, although this can be ANY string)
        var createUserRef = new Firebase("https://<your-firebase>.firebaseio.com", "createUser");

        // and let use that ref to authenticate and get the uid (note that our other ref will still be authenticated as an admin)
        createUserRef.authWithPassword({
          email: <email>,
          password: <password>
        }, function(error, authData) {
          if (error !== null) {
            // Here is the uid we are looking for
            var uid = authData.uid;
          }
        });
      }
    });
  }
});

      

Please note that we will soon release a new version of Firebase that will return the uid in the createUser () callback. This slightly hacky workaround won't be needed at this point.

0


source







All Articles