我正在尝试在Angular 6应用上使用Firebase实现isLoggedIn方法,并且由于firebase用户是“ subcription”对象而遇到了问题,并且我试图像常规方法isLoggedIn()一样使用它。
这是我到目前为止的内容:
export class AngularFireAuthService {
private firebaseUser$: Observable<firebase.User>;
constructor(private angularFireAuth: AngularFireAuth) {
this.firebaseUser$ = angularFireAuth.authState;
}
getFirebaseUser() {
return this.firebaseUser$;
}
register(user: any) {
return this.angularFireAuth.auth.createUserWithEmailAndPassword(user.email, user.password)
}
login(user: any) {
return this.angularFireAuth.auth.signInWithEmailAndPassword(user.email, user.password)
}
logout() {
return this.angularFireAuth.auth.signOut();
}
resetPassword(email: string) {
return this.angularFireAuth.auth.sendPasswordResetEmail(email);
}
isLoggedIn() {
//not sure how to do this.
//I was trying to do something like this:
// this.firebaseUser$.subscribe((user) => {
// if (user) {
// return true;
// } else {
// return false;
// }
// });
}
}
Is there a way for me to implement an isLoggedIn method, so that I could just simply call it in any of my components? I tried googling this and it seems like the way most people are doing it is to subscribe to the firebaseUser$ object, and if the result is not null, then assume the user is logged in. But I was wondering if I could get around having to subscribe in each of my components and instead just call a isLoggedIn
method that would simply return true or false, like this:
if (this.angularFireAugthService.isLoggedIn()) {
//do something
}