Fixed token handling for cases where options is null

This commit is contained in:
Jess Sightler 2016-10-03 18:08:36 -04:00
parent 5b1b63e364
commit cab9740221
2 changed files with 8 additions and 2 deletions

View file

@ -29,4 +29,4 @@ import {KeycloakHttp} from "./keycloak.http";
],
bootstrap: [ AppComponent ]
})
export class AppModule {}
export class AppModule {}

View file

@ -1,5 +1,5 @@
import {Injectable} from "@angular/core";
import {Http, Request, ConnectionBackend, RequestOptions, RequestOptionsArgs, Response} from "@angular/http";
import {Http, Request, ConnectionBackend, RequestOptions, RequestOptionsArgs, Response, Headers} from "@angular/http";
import {KeycloakService} from "./keycloak.service";
import {Observable} from 'rxjs/Rx';
@ -14,6 +14,7 @@ export class KeycloakHttp extends Http {
}
private setToken(options: RequestOptionsArgs) {
if (options == null || KeycloakService.auth == null || KeycloakService.auth.authz == null || KeycloakService.auth.authz.token == null) {
console.log("Need a token, but no token is available, not setting bearer token.");
return;
@ -26,6 +27,11 @@ export class KeycloakHttp extends Http {
let tokenPromise:Promise<string> = this._keycloakService.getToken();
let tokenObservable:Observable<string> = Observable.fromPromise(tokenPromise);
let tokenUpdateObservable:Observable<any> = Observable.create((observer) => {
if (options == null) {
let headers = new Headers();
options = new RequestOptions({ headers: headers });
}
this.setToken(options);
observer.next();
observer.complete();