Denial of result of promise on guard

I have an angular2 c protector canActivate()that calls a service for isLoggedIn()and returns a promise, which is then resolved, and the route is handled accordingly.

However, I am trying to do the opposite, see when the user is not logged in and it does not work.

I tried something just as simple (adding an operator!), Hoping this would work:

@Injectable()
export class AuthGuard implements CanActivate {
    constructor(private authService: AuthService) {}

    canActivate() {
        return !this.authService.isLoggedIn();
    }
}

However, this always returns false, and the route is never activated.

This is a relevant excerpt from my function isLoggedIn():

isLoggedIn(): Promise<Boolean> {
  var component = this;
  return new Promise((resolve, reject) => {       
      component.queryForUser((user) => {
        resolve(user != null);
      });
    }
  });
}

If the user is not equal null, then he is logged in and the promise is resolved using true. Otherwise, a lie.

, , , isNotLoggedIn(), , , : canActivate()?

+4
2

return !this.authService.isLoggedIn() - , JS. this.authService.isLoggedIn() . !this.authService.isLoggedIn() .

canActivate() {
    return this.authService.isLoggedIn().then(result => !result);
}

async canActivate() {
    return !(await this.authService.isLoggedIn());
}

await ... .

+6

, , :

canActivate() {
    return this.authService.isLoggedIn().then(loggedIn => !loggedIn);
}
+3

Source: https://habr.com/ru/post/1674148/


All Articles