canActivate should return Observable not a Subscription . If you call .subscribe() you will get a Subscription , so we use .map() .
To handle the error, use .catch()
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable<boolean> { return this.appService.isValidUser() .map(data => data.authenticated) .catch(_ => Observable.of([false])); }
Do not forget to import all operators
import 'rxjs/add/operator/catch'; import 'rxjs/add/operator/map'; import "rxjs/add/observable/of";
source share