【发布时间】:2021-08-15 12:51:19
【问题描述】:
我正在关注此this 以在 Angular HTTP 拦截器中添加 AWS 访问令牌,但代码进入无限循环,我收到 net::ERR_INSUFFICIENT_RESOURCES 错误。
显示单个字符的token值的console.log。
提前谢谢你
这里是代码
service.ts
getAccessToken(): Observable<string> {
return from(Auth.currentSession()).pipe(
switchMap(session => from(session.getAccessToken().getJwtToken()))
);
}
interceptor.ts
/* eslint-disable no-param-reassign */
import { Injectable } from '@angular/core';
import {
HttpEvent,
HttpInterceptor,
HttpHandler,
HttpRequest,
} from '@angular/common/http';
import { Observable } from 'rxjs';
import { UserService } from 'app/user.service';
import { switchMap } from 'rxjs/operators';
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor(public userService: UserService) {}
intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
return this.userService.getAccessToken().pipe(
switchMap(jwtToken => {
console.log(jwtToken);
// clone the request to add the new header.
req = req.clone({
headers: req.headers.set('Authorization', `Bearer ${jwtToken}`),
});
if (!req.headers.has('Content-Type')) {
req = req.clone({
headers: req.headers.set('Content-Type', 'application/json'),
});
}
return next.handle(req);
})
);
}
}
【问题讨论】:
标签: angular rxjs angular8 aws-amplify rxjs6