【问题标题】:Angular Project: How to get instance of secondary firebase app?Angular Project:如何获取辅助 firebase 应用程序的实例?
【发布时间】:2018-08-13 13:34:51
【问题描述】:

在我的 Angular 应用程序中,我遇到了与提出以下问题的人相同的问题: Firebase kicks out current user

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

显然,这可以通过创建第二个身份验证引用并使用它来创建用户(请参阅上面链接的问题的批准答案):

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

但是那我怎么能在代码中引用二级firebaseApp呢?


更新:

按照 Frank van Puffelen 建议的答案,我执行了以下操作:

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

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


 } 

但是,在尝试添加新用户时,我收到以下错误消息:


更新 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


 } 

【问题讨论】:

    标签: javascript angular firebase firebase-authentication


    【解决方案1】:

    您可以将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");
    

    【讨论】:

    • 非常感谢您提出的答案...不幸的是,我现在收到另一条错误消息(请参阅我的帖子的更新)
    • 在你分享的截图中你称之为SecondaryApp。您注册应用程序并用于备份应用程序的名称应该匹配。
    • 非常感谢您的帮助...现在已解决(见更新 2)
    猜你喜欢
    • 2021-03-21
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-11
    相关资源
    最近更新 更多