我正在使用angular-firebase创建一个简单的登录,并使用电子邮件密码作为身份验证方法。但即使在使用af.auth.signout()方法后,用户也不为空,并且在重新加载页面时仍然登录。
请输入以下代码段:
constructor(public af: AngularFireAuth,private router: Router) {
this.af.authState.subscribe(auth => {
if (auth != null) {
this.authenticated = true;
}
}
)
}
login() {
this.af.auth.signInWithEmailAndPassword("email","password");
this.authenticated = true;
}
logOut() {
this.af.auth.signOut();
this.authenticated = false;
}
因此,当我调用login()时,用户最初为null并且已登录。但是当调用logOut()方法时,该方法成功执行,但构造函数被调用firebase.user(我的代码中的auth) 为什么logOut()方法(this.af.auth.signOut())没有使用户为空并退出?
答案 0 :(得分:1)
您需要取消订阅可观察量,否则会发生内存泄漏
private sub: any;
constructor(public af: AngularFireAuth,private router: Router) {
this.sub =this.af.authState.subscribe(auth => {
if (auth != null) {
this.authenticated = true;
}
}
)
}
logOut() {
this.af.auth.signOut();
this.authenticated = false;
}
ngOnDestroy() {
this.sub.unsubscribe();
}
答案 1 :(得分:0)
signOut()返回一个Promise,因此您最好编写:
logOut() {
this.fb.auth.signOut()
.then(() => {
this.authenticated = false;
})
.catch(error => {
console.log(error);
});
}