I'm trying to return the current authentication status in a service method. (The code works except for the return.) I'm not familiar with the call to .then inside .map and I can't figure out how to return an Observable.
public getSignedInStatus(): Observable<boolean> {
this._gauth.getAuth().map(auth => {
auth.then(function() {
return Observable.of(auth.isSignedIn.get());
});
});
}
In the docs, there's a subscribe call. I tried changing it to .map but it didn't help.
this._gauth.getAuth()
.subscribe(auth => {
The error is "A function whose declared type is neither 'void' nor 'any' must return a value."
I want to be able to call the method from any component. Something like this:
return this._userService.getSignedInStatus()
.map(
signedIn => {
if (signedIn === true) {
return true;
} else {
return false;
}
},
error => console.error('Error getting user status')
);
And in an AuthGuard, like this:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
this._userService.getSignedInStatus()
.subscribe( response => {
const status = response.isSignedIn.get();
if (status === true) {
return true;
} else {
this.router.navigate(['/signin']);
return false;
}
},
error => {
console.error('Error getting user status');
return false;
});
}
}
this._gauth.getAuth()
? if the return value ofpublic getSignedInStatus():
isObservable<boolean>
then you need to usesubscribe()
in your component class to consume the service likereturn this._userService.getSignedInStatus() .subscribe(
– Niladrimap
? – Niladri