angular - 在 Angular 10 中获取新的访问令牌后订阅未执行
问题描述
我已经在 API中使用access-token
和机制进行身份验证。refresh-token
当我的访问令牌过期时,我401 Unauthorized
在 API 中遇到错误。所以我正在调用请求,它使用正常工作的刷新令牌返回新的访问令牌。但问题是.subscribe
函数内部编写的代码没有被重新执行。
getActivePlan() {
this.myPlanExplainerActive = true;
var route = this.router.url;
this.ps.getUserPlan().subscribe(res => { <========== Getting 401 Unauthorized error when accss-token expired
var plans = res.data.plans; <===== After getting new access-token execution never goes inside.
var activeplan = plans.find(i => i.plan.isActive == true);
if (activeplan != undefined) {
this.activePlanId = activeplan.plan.planId;
if (route != "/planparser/new-pdf-upload" && this.activePlanId != '') {
this.router.navigate(['planparser/view-plan/', this.activePlanId]);
}
}
else {
this.router.navigate(['planparser/new-pdf-upload/']);
}
});
}
在这里,当我在 getUserPlan() API 中收到 401 Unauthorized 错误时,我的拦截器会命中并获取新的访问令牌。但是,执行永远不会进入订阅内部,因此进一步的代码不会被执行。
intercept(request: HttpRequest<unknown>, next: HttpHandler):
Observable<HttpEvent<unknown>> {
let clonedRequest = request.clone();
if (request.url.indexOf("/auth/oauth/token") == -1) {
const auth = this.inject.get(AuthService);
let cloned;
if (auth.getToken()) {
cloned = request.clone({
setHeaders: {
Authorization: `Bearer ${auth.getToken()}`,
'Accept': 'application/json'
}
});
}
else {
cloned = request.clone();
}
return next.handle(cloned).pipe(
catchError(
(async (error: HttpErrorResponse) => {
if (error.status === 401) {
return this.handle401Error(request, next);
} else {
return throwError(error);
}
})
)
) as any;
}
else if (request.url.indexOf('/auth/oauth/token') != -1) {
clonedRequest = request.clone({
setHeaders: {
Authorization: `Basic ${window.btoa('ClientId' + ':' + 'secret')}`,
'Content-Type': "application/x-www-form-urlencoded"
}
});
}
return next.handle(clonedRequest)
.pipe(
catchError(
((error: HttpErrorResponse) => {
if (error.status !== 401) {
this.msgs.sendMessage({ command: 'Sorry, we are unable to process your plan. Please upload your correct NDIS plan PDF again.' });
}
return throwError(error);
})
)
);
}
private handle401Error(request: HttpRequest<any>, next: HttpHandler) {
if (!this.isRefreshingToken) {
this.isRefreshingToken = true;
this.tokenSubject.next(null);
return this.authService.refreshTokenAPIcall().pipe(
switchMap((token: any) => {
this.isRefreshingToken = false;
this.tokenSubject.next(token.access_token); <============== getting new token
return next.handle(this.addToken(request))
})).subscribe(res => {
console.log(res);
});
} else {
return this.tokenSubject.pipe(
filter(token => token != null),
take(1),
switchMap(jwt => {
return next.handle(this.addToken(request));
}));
}
}
解决方案
推荐阅读
- loops - 在 Ansible 中构建循环
- javascript - 如何引用数组元素作为 .backgroundImage 的目标?
- flutter - 一个接一个地为相同类型的小部件设置动画的更好方法?
- amazon-web-services - 上传文件或发送电子邮件 aws 时出现身份验证错误
- list - Flutter:将列表从一页传递到另一页
- xamarin - 如何从 GenericRepository 调用 MessagingCenter.Send
- java - 将现有的 gradle 项目导入 intelli J idea
- django - 如何对我的另一个视图进行 API 调用?
- stm32 - NUCLEO-STM32F401RE 板上的 ADC 在输入信号超过 10kHz 时无法工作
- javascript - 在选择时删除一个类并将类添加给其他人