我正在使用Angh4,使用authservice和http服务。
当我尝试刷新jwt令牌时,通过在每次请求服务器之前检查令牌到期。为了避免循环依赖,我在auth.service.ts中使用注入器:
constructor(// private http: HttpClient,
inj: Injector) {
setTimeout(() => { this.http = inj.get(HttpClient) })
}
但是在调用http.somefunction()时会出现错误 错误TypeError:无法读取未定义
的属性'somefunction'我的http.service.ts如下
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { tokenNotExpired, JwtHelper } from 'angular2-jwt';
import { JwtAuthService } from './auth.service'
@Injectable()
export class HttpClient {
jwtHelper: JwtHelper = new JwtHelper();
constructor(private http: Http, private _jwtAuthService: JwtAuthService) { }
createAuthorizationHeader(headers: Headers) {
let token = '';
if (sessionStorage.getItem('token')) {
token = sessionStorage.getItem('token')
}
if (token && typeof token === 'string') {
if (this.jwtHelper.isTokenExpired(token)) {
// get aws token using refresh_token and get jwt token
this._jwtAuthService.refreshToken().then(refreshedtoken => {
headers.append('Authorization', 'Bearer ' + refreshedtoken);
});
}
else {
headers.append('Authorization', 'Bearer ' + token);
}
}
}
getwithheader(url, header) {
const headers = new Headers();
headers.append('Authorization', 'Bearer ' + header);
this.createAuthorizationHeader(headers);
return this.http.get(url, {
headers: headers
});
}
get(url) {
const headers = new Headers();
this.createAuthorizationHeader(headers);
return this.http.get(url, {
headers: headers
});
}
awspost(url, data) {
const headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
return this.http.post(url, data, {
headers: headers
});
}
post(url, data) {
const headers = new Headers();
this.createAuthorizationHeader(headers);
return this.http.post(url, data, {
headers: headers
});
}
}
和auth servce如下
import { Injectable, Injector } from '@angular/core';
import { SharedService } from './shared.service';
import { ErrorHandlerService } from './error-handler.service';
import { HttpClient } from './http.service';
import { Observable } from 'rxjs/rx';
import { Http, Response } from '@angular/http';
import * as AWS from 'aws-sdk/global';
import 'rxjs/add/operator/toPromise';
import { environment } from '../../environments/environment';
import { tokenNotExpired, JwtHelper } from 'angular2-jwt';
@Injectable()
export class JwtAuthService {
BASE_URL = this._sharedService.BASE_URL;
AWS_ACCESS_KEY = 'test';
http: HttpClient;
jwtHelper: JwtHelper = new JwtHelper();
constructor(// private http: HttpClient,
private _sharedService: SharedService,
private _errorHandler: ErrorHandlerService,
inj: Injector) {
setTimeout(() => { this.http = inj.get(HttpClient) })
}
getAwsToken(code): any {
const body = 'grant_type=' + environment.AWS_GRANT_TYPE + '&client_id=' + environment.AWS_CLIENT_ID + '&redirect_uri=' + environment.AWS_REDIRECT_URI + '&code_verifier=' + code;
return this.http.awspost(environment.AWS_BASE_URL + environment.AWS_TOKEN_END_POINT + '?code=' + code, body).toPromise()
.then((resp: Response) => resp.text())
.catch(err => this._errorHandler.handleError(err));
}
getToken(token): any {
return this.http.getwithheader(this.BASE_URL + '/token', token).toPromise()
.then((resp: Response) => resp.text())
.catch(err => this._errorHandler.handleError(err));
}
getRefreshAWSToken(refreshToken): any {
const body = 'grant_type=refresh_token' + '&client_id=' + environment.AWS_CLIENT_ID + '&redirect_uri=' + environment.AWS_REDIRECT_URI + '&code_verifier=' + refreshToken;
return this.http.awspost(environment.AWS_BASE_URL + environment.AWS_TOKEN_END_POINT, body).toPromise()
.then((resp: Response) => resp.text())
.catch(err => this._errorHandler.handleError(err));
}
refreshToken(): any {
this.getRefreshAWSToken(sessionStorage.getItem('refreshToken'))
.then(resp => {
this.getToken(JSON.parse(resp).id_token)
.then(jwtresp => {
sessionStorage.setItem('user', JSON.stringify(this.jwtHelper.decodeToken(jwtresp)));
sessionStorage.setItem('token', JSON.parse(jwtresp).token);
return JSON.parse(jwtresp).token
});
});
}
}
这里缺少什么?