0

I have created an internal application whereas users are created by the administrator that is using the web application. However, whenever I create a new user, firebase will automatically change the user to the user I have logged in with which is not what I want. Does anyone have any idea how I am able to create a user account in Firebase Auth without authenticating into that account immediately after I have created it and stay in the account I am logged in with?

To Further elaborate, I am receiving this error:

Uncaught (in promise) FirebaseError: Firebase: Firebase App named '[DEFAULT]' already exists with different options or config (app/duplicate-app).

Find the code in attached picture Current Code

der
  • 69
  • 7

1 Answers1

0

One workaround that comes to mind would be to instantiate separate firebase apps in your code. The main firebase app would be the real one that the administrator signs in on, and the others would be created on demand to be used for creating the new user, and then thrown away afterwards. Since the account creation is happening on the other apps, it shouldn't log the admin out of the main app.

For example:

import { initializeApp } from 'firebase/app';
import { getAuth, signInWithEmailAndPassword } from 'firebase/auth';

let counter = 0; // Used to give every app a unique name
function createUser(email, password) {
  counter++;
  const app = initializeApp({
    apiKey: // ...
    authDomain: // ...
    //etc
  }, '' + counter);

  const auth = getAuth(app);
  signInWithEmailAndPassword(auth, email, password);
  // app never gets used again after this. 
}
Nicholas Tower
  • 72,740
  • 7
  • 86
  • 98
  • Hi, I updated my question above. I have already tried this method and I am getting an error – der Mar 28 '22 at 08:59
  • You need to give each app a unique name, like in my example. – Nicholas Tower Mar 28 '22 at 11:33
  • Hi Nicholas, thanks for the reply! I tried that, i have the default which is app then the app i initialise for this function is called secondaryapp. Is this what you meant? – der Mar 29 '22 at 02:06
  • When you call initializeApp, you can pass a second parameter into it. This is the name of the app. In my example, the name is `'' + counter`, which ensures it's a unique name every time. If you don't pass a name in, then firebase will initialize the default app. You can only initialize an app once, so if you try to initialize the same app, you will get the error that you showed. – Nicholas Tower Mar 29 '22 at 02:53