刷新令牌HTTPInterceptor问题

时间:2018-02-22 20:04:04

标签: angular typescript angular-http-interceptors

我已经坚持了一段时间了。出于某种原因,我的拦截器会刷新令牌(甚至会在尝试刷新时调用失败的端点);但是,在我创建另一个请求之前,我的模型不会更新。

下面是我的HTTPInterceptor代码:

private refreshTokenInProgress = false;
private tokenSubject: BehaviorSubject<any> = new BehaviorSubject<any>(null);
constructor(private auth: AuthService, private http: HttpClient) { }

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    // Rewrite API url/content-type depending on environment (rewrite only ONCE)
    request = request.clone({
        url: environment.apiUrl + request.url,
        setHeaders: {
            "Accept": "application/json",
            "Content-Type": "application/json; charset=utf-8"
        }
    });

    request = this.generateAuthHeaders(request);
    return next.handle(request).catch((error: HttpErrorResponse) => {
        // Make sure we only attempt to refresh when we have an access token
        if (error.status === 401 && this.auth.getAccessToken()) {
            // If we are currently awaiting a token refresh, don't attempt to refresh
            if (!this.refreshTokenInProgress) {
                this.refreshTokenInProgress = true;
                this.tokenSubject.next(null);

                // Attempt to refresh tokens
                return this.auth.refreshToken().switchMap((result: any) => {
                        // Set new tokens if result, otherwise, logout
                        if (result) {
                            this.auth.setAccessToken(result.accessToken);
                            this.auth.setRefreshToken(result.refreshToken);
                            this.tokenSubject.next(result);
                            this.refreshTokenInProgress = false;
                            return next.handle(this.generateAuthHeaders(request));
                        }
                        this.auth.logout();
                        return Observable.empty();
                    }, (err: HttpErrorResponse) => {
                        // Error while attempting to refresh our tokens, logout
                        if (err.status === 401) {
                            this.auth.logout();
                        }
                        return Observable.throw(err);
                    });
            } else {
                return this.tokenSubject.filter((result) => result !== null).take(1).switchMap((result) => {
                    return next.handle(this.generateAuthHeaders(request));
                });
            }
        }
        return Observable.throw(error);
    });
}

generateAuthHeaders(request: HttpRequest<any>): HttpRequest<any> {
    // If we are authenticated, send Authentication header
    const accessToken = this.auth.getAccessToken();
    if (accessToken) {
        return request.clone({
            setHeaders: {
                Authorization: "Bearer " + accessToken
            }
        });
    }
    return request;
}

以及与以下图片有关的代码:

setPage(pageInfo) {
    this.loading = true;
    this.usersService.getUsers(pageInfo.offset, pageInfo.limit).subscribe((pagination) => {
        this.pagination = pagination;
        this.rows = pagination.docs;
        this.loading = false; // None of these get called
    }, (error: HttpErrorResponse) => {
        this.loading = false; // None of these get called
    });
}

enter image description here

正如您所看到的那样,似乎工作正常,但loading永远不会设置为false,这意味着它无法正常工作。

0 个答案:

没有答案