Angular Project:如何获取辅助Firebase应用程序实例?

时间:2018-08-13 13:34:52

标签: javascript angular firebase firebase-authentication

在我的角度应用程序中,我和问以下问题的人有相同的问题:     Firebase kicks out current user

我希望能够添加新的用户帐户,而不会踢出当前用户(=创建新用户帐户的管理员)。

很明显,这可以通过创建第二个auth引用并将其用于创建用户来实现(请参见上面链接的问题的已批准答案):

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();
});

在回答之后,我尝试了以下操作:

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();
        });

}

问题是,这只能运行一次。之后,无法初始化辅助firebaseApp,因为它已经被初始化。

所以我认为,与其在registerUser()方法内部初始化辅助应用程序,不如我应该做类似的事情(在app.module.ts文件中):

enter image description here

但是我该如何在代码中引用辅助firebaseApp?


更新:

在弗兰克·范·普菲伦(Frank van Puffelen)建议的答案之后,我做了以下事情:

1。)在app.module.ts文件中:

enter image description here

2。)在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


 } 

但是,当尝试添加新用户时,出现以下错误消息:

enter image description here


更新2:

弗兰克·范·普菲伦(Frank van Puffelen)指出了我犯的一个愚蠢的错误:我向该应用程序注册的名称与用于备份该应用程序的名称不匹配。 更正此错误消息后,错误消息消失了。 但是,创建新帐户后,当前用户仍然被踢出。

...最终起作用的是以下内容:

(文件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


 } 

1 个答案:

答案 0 :(得分:3)

您可以将secondaryApp设为全局,然后在代码中的其他位置引用它。像这样:

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");

实际上,我会将辅助应用程序的整个配置拉到一个全局范围内,因为它只能运行一次。所以:

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) {
            ...
        });
}

您始终可以按其名称查找FirebaseApp实例。所以在你的胸膛里:

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