How can I transform an Observable<Response> into Observable<boolean>. This is inside a route guard.
Code:
canActivate(next: ActivatedRouteSnapshot,state: RouterStateSnapshot): Observable<boolean> {
let obs = this.http
.get(environment.apiUrl + '/admin', { withCredentials: true})
.map<Response, boolean>((res: Response) => { return true; });
return obs;
}
Doesn't work. I don't understand the error message:
The 'this' context of type 'Observable<Response>' is not assignable to method's 'this' of type 'Observable<Response>'.
Type 'Response' is not assignable to type 'Response'. Two different types with this name exist, but they are unrelated.
Property 'body' is missing in type 'Response'.
Edit
After wasting 1 hour trying to comprehend the error message I instead used this. Seems to work:
canActivate(next: ActivatedRouteSnapshot,state: RouterStateSnapshot): Observable<boolean> {
debugger;
let subject = new Subject<boolean>();
let authenticated = this.http.get(environment.apiUrl + '/admin/access', { withCredentials: true})
authenticated.subscribe((res) => {
if (res.status == 200)
subject.next(true);
else
subject.next(false);
}, () => {
subject.next(false)
}, () => {
subject.complete();
});
return subject;
}