我正在开发一个与Firebase连接的Angular应用程序。如果我在浏览器中有2个应用程序实例并从一个实例中注销,如何通知另一个实例该用户不再经过身份验证并且应取消订阅Firebase。
我提供了一个stackblitz,它订阅了Firebase的真实实例。单击sign in
时,它会登录并获取文档。这会重新创建场景,但由于它只在控制台上输出日志,因此它仍然无法按照实际项目的预期工作。
Stackblitz. To recreate, open 2 instances of it, sign in one of them and signout.
我不确定这是否是你需要的,因为我无法重现错误。虽然发布它无论如何。
export class SomeComponentOrService{
private subscription: Subscription;
constructor(private afAuth: AngularFireAuth){
this.subscription = afAuth.subscribe(res => {
if(isNullOrUndefined(res){
this.subscription.unsubscribe();
}
});
}
}
添加可能是问题的另一个答案。
this.afAuth.auth.signOut();
auth
是现在的州。如果用户已注销,则当前状态可能为null。简单的空检查可能会修复您的错误。
if(!isNullOrUndefined(this.afAuth.auth){
this.afAuth.auth.signOut();
}