我有一个 AuthService,主要有两种方法:
getAuthToken(返回 Promise,因此可以延迟调用/在单个集合上阻塞等待多次调用)
refreshToken(还返回一个 Promise,使用原始 JWT 上可用的刷新令牌来请求新的身份验证令牌)
我想自动
这是代码:
import { HttpEvent, HttpHandler, HttpHeaders, HttpInterceptor, HttpRequest } from "@angular/common/http";
import { from, Observable } from "rxjs";
import { Injectable } from "@angular/core";
import { AuthService } from "./auth.service";
@Injectable()
export class AuthHttpInterceptor implements HttpInterceptor {
constructor(
private _authService: AuthService,
) {
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return from(this.addBearerToken(req, next));
}
private async addBearerToken(req: HttpRequest<any>, next: HttpHandler): Promise<HttpEvent<any>> {
const token = …Run Code Online (Sandbox Code Playgroud)