developer1
developer1

Reputation: 557

Observable<boolean> is not working properly

I'm trying to implement a guard using userService to get the needed info. Currently the UserService looks like this:

  getUserDetails(): Observable<User> {
    this.requestUrl = `${configs.bgwssApi}v1/user/details`;
    return this.http.get<User>(this.requestUrl).pipe(catchError(this.handleError));
  }

  isBoardMember(): Observable<boolean> {
    this.getUserDetails().pipe(
      map(data => {
        if (data.userRole !== 'MEMBER') {
          return of(false);
        }
        console.log(data); //Not printed
      })
  );
    return of(true);
  }

The getUserDetails works fine, it returns the needed info. But isBoardMember always return true, as I noticed it doesn't even check the userRole. What could be the problem here? How to fix the isBoardMember so it would return the correct value, because later my guard performs a check like this, which is working fine btw:

canActivate(route: ActivatedRouteSnapshot,
          state: RouterStateSnapshot): Observable<boolean> {
return this.userService.isBoardMember().pipe(
  map(data => {
    if (data === false) {
      this.router.navigate(['main']);
      return false;
    }
    return true;
  })
);

}

Upvotes: 1

Views: 151

Answers (1)

Igor
Igor

Reputation: 62228

You need to return the observable.

isBoardMember(): Observable<boolean> {
    return this.getUserDetails().pipe(map(data => {
        return !(data.userRole !== 'MEMBER');
    }));
}

Upvotes: 3

Related Questions