2

user signs in. I make a call to create new user with

firebase.auth().createUserWithEmailAndPassword(email, password)

it successfully creates new firebase user. Before this call firebase.auth().currentUser.uid gives different uid then after the call. So active user changes.

Is there any way for active user create another user and still be active user?

I know one way to work this around:

Save user's email & password to memory and after creating user re-login back to original user, but it requires saving password and i don't like it.

firebase.auth().createUserWithEmailAndPassword(email, password)
      .then(user => {
        // login back to main account
        firebase.auth().signInWithEmailAndPassword(current_user_email, current_user_password).then(original_user => {

          const userRef = firebase.database().ref().child(`users/${user.uid}`);
          userRef.set({
            ...
Lukas Liesis
  • 24,652
  • 10
  • 111
  • 109

1 Answers1

1

To add a new Firebase user without automatically switching to that user you can use a secondary Firebase instance to create the user. For example:

var firebaseConfig = {
  apiKey: "asdasdlkasjdlkadsj",
  authDomain: "asdfg-12345.firebaseapp.com",
  databaseURL: "https://asdfg-12345.firebaseio.com",
  storageBucket: "asdfg-12345.appspot.com",
};

firebase.initializeApp(firebaseConfig);  // Intialise "firebase"
var fbAdmin = firebase.initializeApp(firebaseConfig, "Secondary"); // Intialise "fbAdmin" as instance named "Secondary".

To create the user:

fbAdmin.auth().createUserWithEmailAndPassword(email, password)
 .then(function(newUser) {
  // Success. 
  // Log out.
  fbAdmin.auth().signOut()
   .then(function () {
    // Sign-out successful.
    console.log('fbAdmin signed out.');
  }, function (error) {
    // An error happened.
    console.log('Error siging out of fbAdmin.');
    console.log(error);
  });
}, function (error) {
  // There's a problem here.
  console.log(error.code);
  console.log(error.message);
})

The above is for plain JavaScript. It's a bit trickier if you're using auto configuration to manage multiple environments. (See also Reserved URLs).

If you're using node.js you can use the Firebase Admin Node.js SDK. For requirements and set up information see the installation instructions.

Edit: Pretty much identical to this answer.

Community
  • 1
  • 1
imclean
  • 339
  • 2
  • 7