【发布时间】: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