G
G
Ghoulll2019-12-24 14:04:14
Angular
Ghoulll, 2019-12-24 14:04:14

How to implement this Guard correctly?

Help implement this guard. The problem is that always false is returned. (getUser makes a request to the test api and gets the object).

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
    return this.auth.getUser().subscribe( u => {
      this.person = u;
      if (this.person.err == 0 ) {
        return true;
      } else {
        return false;
      }
    });
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2019-12-24
@Ghoulll

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
  return this.auth.getUser().pipe( map(user => user.err == 0));
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question