I am currently facing an issue with retrieving the authentication status in a service method. Everything seems to be working fine except for the return statement. I am struggling with the usage of .then inside .map and I am unable to figure out how to return an Observable.
public getAuthenticationStatus(): Observable<boolean> {
this._authService.getAuth().map(auth => {
auth.then(function() {
return Observable.of(auth.isLoggedIn());
});
});
}
I have looked through the documentation and came across a subscribe call. Even after changing it to .map, I still couldn't resolve the issue.
this._authService.getAuth()
.subscribe(auth => {
The error message I keep encountering is
"A function whose declared type is neither 'void' nor 'any' must return a value."
I aim to make this method accessible from any component, similar to the following:
return this._userService.getAuthenticationStatus()
.map(
authenticated => {
if (authenticated === true) {
return true;
} else {
return false;
}
},
error => console.error('Error fetching user status')
);
Furthermore, I intend to utilize this in an AuthGuard as shown below:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
this._userService.getAuthenticationStatus()
.subscribe( response => {
const status = response.isLoggedIn();
if (status === true) {
return true;
} else {
this.router.navigate(['/login']);
return false;
}
},
error => {
console.error('Error fetching user status');
return false;
});
}
}