【发布时间】:2021-08-21 05:57:22
【问题描述】:
我正在尝试使用一个值来检查令牌是否已过期或未在拦截器中。 Token Expired 值出现在响应标头中。 screenshot of response header
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const reqWithCredentials = req.clone({withCredentials: true});
return next.handle(reqWithCredentials)
.pipe(map(resp => {
// on Response
if (resp instanceof HttpResponse) {
// Do whatever you want with the response.
return resp;
}
}),
catchError(error => {
if (error instanceof HttpErrorResponse) {
if (error.status === this._appConstant.ErrorCodes.Unauthorized || error.status === this._appConstant.ErrorCodes.Forbidden) {
if (error.headers.has('Token-Expired')) {
this.router.navigate(["/tokenexpired"]);
}
else {
this.router.navigate(["/unauthorized"]);
}
}
else {
return throwError(error);
}
}
})
);
}
【问题讨论】:
标签: angular http angular-http-interceptors