4

Im using an angular application with firebase, and i have the feature that allows the admin user to create more users this is the code im using in my AuthService

registerEmployeeByEmail(user: User, password: string): Observable<void> {
    return from(this.afAuth.auth.createUserWithEmailAndPassword(user.email, password).then(credentials => {

      user.id = credentials.user.uid;
      this.db.doc(this.dbPath + user.id).set(user);    
    }));
  }

the problem is that right after the newly user is being logged in, the idea is not to log him in automatically...

naoru
  • 2,149
  • 5
  • 34
  • 58

1 Answers1

1

Ideally, you should use the Firebase Admin SDK to create users with admin privileges. This will also avoid any throttling due to too many requests coming from the same IP address and prevents non-admin users from using this portal to create users too.

However, if you absolutely need this (and I don't recommend it), you simply initialize another FirebaseApp instance to create the users. That would keep the currentUser on the default Auth instance:

const app2 = firebase.initializeApp(config, 'tempApp');
app2.auth().createUserWithEmailAndPassword(email, password)...
bojeil
  • 29,642
  • 4
  • 69
  • 76
  • Do i need ro explicitly close the app2? – naoru Feb 04 '19 at 12:03
  • When you're done with `app2`, sign out the last current user and delete the `app2` FirebaseApp instance. – bojeil Feb 04 '19 at 19:45
  • im still not clear on how to initiallize, on startup im using the following code inside my app.module.ts file AngularFireModule.initializeApp(environment.firebaseConfig) but inside a service, i cannot use this code.what is the type of the firebase variable in your snippet? – naoru Feb 06 '19 at 19:24
  • I implement this on my service. On constructor I instance the new firebase: this.app2 = firebase.initializeApp(environment.firebase, 'tempApp'); -- And I import: import firebase from 'firebase'; -- then, you use it but with this.app2.... I write the code in the next response... – gabrielrincon Feb 22 '22 at 20:16