Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular isAuthenticated调用上的角度刷新访问令牌_Angular_Rxjs - Fatal编程技术网

Angular isAuthenticated调用上的角度刷新访问令牌

Angular isAuthenticated调用上的角度刷新访问令牌,angular,rxjs,Angular,Rxjs,我正在尝试使用刷新令牌设置jwt身份验证。我的身份验证服务中有此方法: isAuthenticated():布尔值{ const jwtHelper=newjwthelperservice(); return!jwtHelper.isTokenExpired(this.getAccessToken()); } 我想这样使用它: isAuthenticated():可观察{ const jwtHelper=newjwthelperservice(); 如果(!jwtHelper.isTokenE

我正在尝试使用
刷新令牌设置
jwt
身份验证。我的
身份验证服务中有此方法:

isAuthenticated():布尔值{
const jwtHelper=newjwthelperservice();
return!jwtHelper.isTokenExpired(this.getAccessToken());
}
我想这样使用它:

isAuthenticated():可观察{
const jwtHelper=newjwthelperservice();
如果(!jwtHelper.isTokenExpired(this.getAccessToken())){
返回(真);
}
返回此。refreshToken();//返回可观察的
}

因此,我希望始终拥有有效的访问令牌,而不是在每次api调用时刷新它(这可能更好),因为如果用户未登录,我在
*ngIf
中使用它来不显示某些组件。因此,如何对
的所有调用执行一次对
refreshToken
的调用?

因此,我决定使用来自此的流。我添加了
app initializer
(调用
refreshttoken
)并将
isAuthenticated
替换为

isAuthenticated(): boolean {
  return this.gerUserData() != null;
}

我认为这个流程更好,因为如果没有带负载的api调用,每次令牌过期时刷新令牌都是无用的。

上面的代码应该可以工作。您只需要使用
async
管道与
*ngIf
组合,因为它返回一个可观察的。是的,我知道,我想使用
async
管道,但如果我有多个
*ngIf
它将导致大量
refreshttoken
调用。所以,我想问的是如何避免这种多次通话。我想在服务端解决它,而不是组件,这样
isAuthenticated
就可以在其他服务中使用