I am trying to create firebase authentication for my app.
Here is the auth.service.ts
private isloggedIn = false;
constructor(private af: AngularFire, private router: Router) {
// this.af.auth.subscribe((auth) => console.log(auth));
this.af.auth.subscribe(user => {
if (user) {
this.isloggedIn = 'true';
} else {
this.isloggedIn = 'false';
}
});
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.af.auth.map((auth) => {
if(auth == null) {
this.router.navigate(['/auth']);
return false;
} else {
return true;
}
}).first()
}
isLoggedIn () {
return this.isloggedIn;
}
What I want to do is to observe for authentication state. If that changes, then this.isloggedIn changes too.
Then how do I get the isloggedin auth.service value in my components.
import { AuthService } from './auth.service';
export class SomeComponent implements OnInit {
constructor(private titleService: Title, private AuthService : AuthService, public toastr: ToastsManager, private af: AngularFire) {
}
ngOnInit() {
this.titleService.setTitle("some component title");
console.log(this.AuthService.isLoggedIn());
}
At the moment, when I use this.AuthService.isLoggedIn() gives me undefined even after user is logged in. What am I doing wrong?