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):
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:
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:
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
}