我对刷新令牌的流程有疑问。
我尝试拨打这个令牌。令牌已过期,并发出第二个刷新令牌的请求。我正在接受回复并使用新令牌重新运行请求。
在尝试使用相同的无效令牌进行多个并行请求之前,一切都很完美。
问题是,如果我使用相同的令牌进行3次并行呼叫,那么第一次呼叫会使令牌对其他2次呼叫无效。
我是否对流量做错了?
import {Injectable} from '@angular/core';
import {Request, XHRBackend, RequestOptions, Response, Http, RequestOptionsArgs, Headers} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import {Router} from '@angular/router';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import {RefreshTokenService} from "../services/refreshToken.service";
@Injectable()
export class HttpService extends Http {
constructor(
backend: XHRBackend,
options: RequestOptions,
private refreshTokenService?: RefreshTokenService,
private router?: Router
) {
super(backend, options);
}
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
if (typeof url === 'string') {
if (!options) {
options = {headers: new Headers()};
}
this.setHeaders(options);
} else {
this.setHeaders(url);
}
return super.request(url, options).catch(this.catchErrors(url, options));
}
private catchErrors(url: string | Request, options?: RequestOptionsArgs) {
return (res: Response) => {
if (res.status === 401 || res.status === 403) {
if ( this.refreshTokenService.wait === false ) {
this.refreshTokenService.wait = true;
return this.refreshTokenService.refreshToken(localStorage.getItem('JWToken'))
.flatMap((result: any) => {
// if got new access token - retry request
if (JSON.parse(result._body).token) {
localStorage.setItem('JWToken', JSON.parse(result._body).token);
this.setHeaders(url);
this.refreshTokenService.wait = false;
return this.request(url, options);
} else {
return Observable.throw(new Error('Can\'t refresh the token'));
}
})
} else {
// TODO... return this only if new token is ok
this.setHeaders(url);
return this.request(url, options);
}
} else {
Observable.throw(res);
}
};
}
private setHeaders(objectToSetHeadersTo: Request | RequestOptionsArgs) {
// add whatever header that you need to every request
objectToSetHeadersTo.headers.set('Authorization', 'Bearer ' + localStorage.getItem('JWToken'));
}
}