I have a route guard like below
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router, private authenticationSvc:
You can catch errors and return Observable<bool>
as follows:
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router, private authenticationSvc: AuthenticationService) { }
canActivate(): Observable<boolean> {
return this.authenticationSvc.getAuthenticatedUser().map(
r => {
if (this.authenticationSvc.isAuthenticated()) {
// logged in so return true
return true;
}
this.router.navigateByUrl('/login');
return false;
})
.catch((error: any) => {
this.router.navigateByUrl('/login');
return Observable.of(false);
});
}