so i'm trying extend authhttp class repository can ask things more relevant use case, namely automatically checking see whether bearer token expired , using refresh token fetch auth token retrying original request. pretty standard oauth 2 stuff. i'm newby angular2, , have run problem di. i've tried bunch of things fix it, can't figure out how fix error.
here's simplified version of customhttp class extending authhttp:
import { injectable } '@angular/core'; import { request, http, requestoptionsargs, headers, response, requestoptions } '@angular/http'; import { authhttp, authconfig, iauthconfig, tokennotexpired } 'angular2-jwt'; import { observable } '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 default authhttp request call (which gets called on every single request btw), , refresh token if can request(url: string | request, options?: requestoptionsargs) : observable<response> { if (typeof url === 'string') { return this.get(url, options); // recursion: transform url string request } // point url 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 this:
import { component } '@angular/core'; import { customhttp } '../../shared/customhttp/index'; import { authconfig } 'angular2-jwt'; /** * class represents 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 parameters authconfig: (?).
anything can suggest out fantastic. i've tried @inject(type_here) every parameter of customhttp class constructor try fix it, , imported different types on place trying fix no dice. ton.
also, if know of library i'm looking out of box i'd consider switching not home-grown.
Comments
Post a Comment