3

In my angular app I have the same problem as the person who asked the following question: Firebase kicks out current user

I want to be able to add a new user account without the current user (= the admin who is creating the new user account) being kicked out.

Apparently, this is possible by creating a second auth reference and use that to create users (see approved answer to the question linked to above):

var config = {apiKey: "apiKey",
    authDomain: "projectId.firebaseapp.com",
    databaseURL: "https://databaseName.firebaseio.com"};
var secondaryApp = firebase.initializeApp(config, "Secondary");

secondaryApp.auth().createUserWithEmailAndPassword(em, pwd).then(function(firebaseUser) {
    console.log("User " + firebaseUser.uid + " created successfully!");
    //I don't know if the next statement is necessary 
    secondaryApp.auth().signOut();
});

Following this answer, I tried the following:

registerUser(authData: AuthData, participantId: string, role: string): Promise<any> {

        let config = {
            apiKey: "API-Key",
            authDomain: "myApp-a7211.firebaseapp.com",
            databaseURL: "https://myApp-a7211.firebaseio.com",
        };

        let secondaryApp = firebase.initializeApp(config, "Secondary");


        return secondaryApp.auth().createUserWithEmailAndPassword(authData.email, authData.password)
        .then(function(firebaseUser) {
            console.log("User " + firebaseUser + " created successfully!");

            secondaryApp.auth().signOut();
        });

}

The problem is that this works only once. After that, initialisation of the secondary firebaseApp is not possible because it has been initialised already.

So I thought, rather than initialising the secondary app inside the registerUser()-method, maybe I should do something like that (in file app.module.ts):

enter image description here

But then how can I refer to the secondary firebaseApp in the code?


UPDATE:

Following the suggested answer by Frank van Puffelen, I did the following:

1.) In file app.module.ts:

enter image description here

2.) In file auth.service.ts:

export class AuthService implements OnDestroy {

    private secondaryApp = firebase.app("Secondary");

    //other code

    registerUser(authData: AuthData): Promise<any> {

        return this.secondaryApp.auth().createUserWithEmailAndPassword(authData.email, authData.password)
        .then(function(firebaseUser) {
            console.log("User " + firebaseUser + " created successfully!");

            this.secondaryApp.auth().signOut();
        });

    }


    //other methods


 } 

However, when trying to add a new user, I get the following error message:

enter image description here


UPDATE 2:

Frank van Puffelen pointed out a silly mistake I had made: the name I registered the app with, and the name used to look the app back up, did not match. After correcting this, the error message disappeared. However, the current user was still kicked out after creating a new account.

... What finally worked was the following:

(file auth.service.ts:)

export class AuthService implements OnDestroy {

    //do not initialise the secondary app in app.module.ts but here:
    private config = {
        apiKey: "API-KEY",
        authDomain: "myApp-a7211.firebaseapp.com",
       databaseURL: "https://myApp-a7211.firebaseio.com",
    };
    private secondaryApp = firebase.initializeApp(this.config, "SecondaryApp");

    //other code

    registerUser(authData: AuthData): Promise<any> {

        return this.secondaryApp.auth().createUserWithEmailAndPassword(authData.email, authData.password)
        .then(function(firebaseUser) {
            console.log("User " + firebaseUser + " created successfully!");

            this.secondaryApp.auth().signOut();
        });

    }


    //other methods


 } 
Michael
  • 799
  • 1
  • 7
  • 16

1 Answers1

5

You can make secondaryApp global and then refer to it from elsewhere in your code. So something like:

let secondaryApp;
registerUser(authData: AuthData, participantId: string, role: string): Promise<any> {

    let config = {
        apiKey: "AIzaSyAY1TWvQtK0tDQWmRzoouRNZzAnf15RG_A",
        authDomain: "myApp-a7211.firebaseapp.com",
        databaseURL: "https://myApp-a7211.firebaseio.com",
    };

   secondaryApp = firebase.initializeApp(config, "Secondary");

In fact, I'd pull the entire configuration of the secondary app out into a global scope, since it is only supposed to run once. So:

let config = {
    apiKey: "AIzaSyAY1TWvQtK0tDQWmRzoouRNZzAnf15RG_A",
    authDomain: "myApp-a7211.firebaseapp.com",
    databaseURL: "https://myApp-a7211.firebaseio.com",
};
let secondaryApp = firebase.initializeApp(config, "Secondary");
registerUser(authData: AuthData, participantId: string, role: string): Promise<any> {    
        return secondaryApp.auth().createUserWithEmailAndPassword(authData.email, authData.password)
        .then(function(firebaseUser) {
            ...
        });
}

You can always look up the FirebaseApp instance by its name. So in your casse:

let secondaryApp = firebase.app("Secondary");
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807