implementing canActivate auth guard in angular

北慕城南 提交于 2019-12-01 14:34:14

The canActivate function must return a boolean, a promise of a boolean, or an observable of a boolean.

In your case, you return nothing. Probably because you ommited the return in your function.

But it wouldn't work if you add it, because then, you would return a subscription, which isn't accepted by the signature.

What you could do is this :

canActivate() {
  return this.authService.loggedIn().map(res => {
    if(!res) {
      this.router.navigate(['/login']);
    }
    return res;
  });
}

With this code, you comply with the signature, and keep your routing logic.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!