gpt4 book ai didi

javascript - Angular 项目 : How to get instance of secondary firebase app?

转载 作者:行者123 更新时间:2023-11-30 09:20:49 25 4
gpt4 key购买 nike

在我的 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 中):

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


}

最佳答案

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

关于javascript - Angular 项目 : How to get instance of secondary firebase app?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51823657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com