Angular Guard doesnt work with rxjs filter

Published

iam trying to use RouteGuard in my app to prevent unauthorized users from accessing it. My can activate method in Guard looks like this:

canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> {
  return this.statusService.appStatus.pipe(filter(status=>{
    return status.selectedAgency!=null;
  }),map(status=>{
       console.log(status)
    if(status.selectedAgency.allowPreEdit){
      return true;
    }
    else{
      return false;
    }
  }))
}

Iam trying to filter default value from appStatus observable, because its null for selectedAgency. The problem is that it doesnt work – if i use filter method, guard just receives nothing – even the console.log in map method doesnt occur. Any ideas what am i doing wrong? Why it doesnt wait for result? StatusService looks like this:

export class StatusService {
  private statusSubj= new BehaviorSubject<AppStatus>({selectedMonth:new Date(new Date().getFullYear(),new Date().getMonth()),selectedAgency:null});


  get appStatus(){
    return this.statusSubj.asObservable();
  }

 ...
}

Source: Angular Questions

Published
Categorized as angular, javascript, rxjs, typescript Tagged , , ,

Answers

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq