【发布时间】:2018-01-17 04:21:50
【问题描述】:
我使用 Firebase 构建了一个 Angular 5 Login,并通过以下方式在控制台中记录了当前用户。 它工作正常,在通过路由加载另一个页面时也是如此。但是当我重新加载页面时,auth 对象消失了 - 但用户保持登录状态(home component.html 中的用户名,例如保持显示)
登录组件.ts
onLoginEmail(): void {
if (this.validateForm(this.email, this.password)) {
this.authService.loginWithEmail(this.email, this.password)
.then(() => this.router.navigate(['/home']))
.catch(_error => {
this.error = _error
this.router.navigate(['/'])
})
}
}
auth.service.ts
loginWithEmail(email: string, password: string) {
return this.afAuth.auth.signInWithEmailAndPassword(email, password)
.then((user) => {
this.authState = user
this.isLoggedIn()
})
.catch(error => {
console.log(error)
throw error
});
}
isLoggedIn() {
this.afAuth.auth.onAuthStateChanged(auth => {
if (auth) {
console.log(auth);
} else {
console.log('User logged out');
}
});
}
(这是一种常见的方法吗?有更好的方法吗?)
【问题讨论】:
标签: angular firebase firebase-authentication angular5