我正在尝试使用HttpInterceptor实现刷新登录,问题是流程不是所需的流程。要获取新令牌,我需要使用用户名和密码进行隐形登录。
我的拦截器是:
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
let token = localStorage.getItem('TOKEN');
let clone: HttpRequest<any>;
if (token) {
let idUser = JSON.parse(this.jwtHelper.decodeToken(token).id);
if (idUser == localStorage.getItem('idUser')) {
if (this.jwtHelper.isTokenExpired(token)) {
this._up.loginInvisibleEmail().subscribe(res => {
console.log("Token Refrescado con Email");
}, error2 => {
//TODO CATCH ERROR
console.log("Error obteniendo refrescando token");
});
};
clone = request.clone({
setHeaders: {
Accept: `application/json`,
'Content-Type': `application/json`,
Authorization: `Bearer ${token}`
}
});
} else {
console.log("idUser diferentes");
this._ctp.cleanStorage();
let nav = this.app.getActiveNav();
nav.setRoot('Login');
}
}else {
console.log("Without TOKEN");
clone = request.clone({
setHeaders: {
Accept: `application/json`,
'Content-Type': `application/json`
}
});
}
console.log("HI INTERCEPTOR");
return next.handle(clone);
}
为我提供新令牌的服务是:
loginInvisibleEmail() {
let postParams = {"email": localStorage.getItem("name"), "password": localStorage.getItem("password")};
let url = URL_SERVICIOS + "/auth";
let headers = new Headers();
headers.append('Content-Type', 'application/json');
let options = new RequestOptions({headers: headers});
return this.http2.post(url, JSON.stringify(postParams), options)
.map(resp => {
let data_resp = resp.json();
if (data_resp.error) {
this._ctp.cleanStorage();
let nav = this.app.getActiveNav();
nav.setRoot('Login');
} else {
localStorage.removeItem("TOKEN");
localStorage.setItem('TOKEN', data_resp.result['access_token']);
}
}, (error: any) => {
this._ctp.cleanStorage();
let nav = this.app.getActiveNav();
nav.setRoot('Login');
});
}
我正在尝试使用此方法删除具有过期令牌的元素,以检查流程。
deleteEducation(id): Observable<any> {
let url = URL_SERVICIOS + '/users/educative-experience/' + id;
// let headers = new HttpHeaders().set('Authorization', 'Bearer ' + localStorage.getItem('TOKEN'));
return this.http
.delete(url)
.map(resp => {
return resp['result'];
})
}
结果流为:
我需要先删除刷新令牌,在这种情况下,我第一次删除它会失败,但是它会刷新令牌,就像最后一步一样;如果第二次删除它,则它会正常工作,因为令牌之前是刷新的。
我该如何在第一次刷新令牌?
谢谢。