so I'm trying to extend the AuthHttp class from this repository so that I can ask it to do things that are more relevant to my use case, namely automatically checking to see whether the bearer token is expired and then using the refresh token to fetch another auth token then retrying the original request. Pretty much standard OAuth 2 stuff. I'm also a newby to Angular2, and have run into a problem with the DI. I've tried a bunch of things to fix it, but just can't figure out how to fix the error.
Here's a simplified version of my CustomHttp class extending AuthHttp:
import { Injectable } from '@angular/core';
import { Request, Http, RequestOptionsArgs, Headers, Response, RequestOptions } from '@angular/http';
import { AuthHttp, AuthConfig, IAuthConfig, tokenNotExpired } from 'angular2-jwt';
import { Observable } from 'rxjs/Rx';
@Injectable()
export class CustomHttp extends AuthHttp {
private tokenUrl = "token_url";
public apiBase = "api_url";
private _configuration: IAuthConfig;
constructor(options: AuthConfig, private xhttp: Http, private _xdefOpts?: RequestOptions) {
super(options, xhttp);
this._configuration = options.getConfig();
}
private refreshToken() : Observable<Response>{
let headers = new Headers();
headers.append('Authorization','basic_auth_token_here');
headers.append('content-type', 'application/x-www-form-urlencoded');
return this.post(
this.tokenUrl,
"refresh_token="+localStorage.getItem("refresh_token")+"&grant_type="+"refresh_token",
{ headers }
);
}
setAuthHeader(req: Request){
req.headers.set(this._configuration.headerName, this._configuration.headerPrefix + this._configuration.tokenGetter());
}
//overload the default authHttp request call (which gets called on every single request btw), and refresh the token if you can
request(url: string | Request, options?: RequestOptionsArgs) : Observable<Response> {
if (typeof url === 'string') {
return this.get(url, options); // Recursion: transform url from String to Request
}
// from this point url is always an instance of Request;
let req: Request = <Request>url;
if (!tokenNotExpired(null, this._configuration.tokenGetter())) {
if (!this._configuration.noJwtError) {
return new Observable<Response>((obs: any) => {
// reauthenticate if has refresh_token
this.refreshToken()
.map(res => res.json())
.map((res) => {
if(res.access_token){
localStorage.setItem('id_token', res.access_token);
localStorage.setItem('refresh_token', res.refresh_token);
this.setAuthHeader(req);
}else{
obs.error(new Error('No JWT present or has expired'));
}
});
});
}
} else {
this.setAuthHeader(req);
}
return this.xhttp.request(req);
}
}
and I'm using it like this:
import { Component } from '@angular/core';
import { CustomHttp } from '../../shared/customHttp/index';
import { AuthConfig } from 'angular2-jwt';
/**
* This class represents the lazy loaded HomeComponent.
*/
@Component({
moduleId: module.id,
templateUrl: 'view.component.html',
styleUrls: ['view.component.css'],
providers: [ CustomHttp, AuthConfig ]
})
export class ViewComponent{
constructor(private customHttp: CustomHttp) {}
public getSomeDataFromApi(){
this.customHttp.get(this.customHttp.apiBase+"protected_resource_url").map(res => res.json()).map(res => {
console.log(res);
});
}
}
The error says: EXCEPTION: Error: Uncaught (in promise): Can't resolve all parameters for AuthConfig: (?).
Anything you can suggest to help out would be fantastic. I've tried @Inject(type_here) with every parameter of the CustomHttp class constructor to try to fix it, and imported different types all over the place trying to fix it but no dice. Thanks a ton.
Also, if you know of a library that will already do what I'm looking for out of the box I'd also consider switching to something not as home-grown.