Created
October 19, 2016 04:57
-
-
Save usmansaleem/30d613fd11ce456590d8b4d7268bbb98 to your computer and use it in GitHub Desktop.
Angular2: Jwt Http Client Wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Injectable} from "@angular/core"; | |
import {Http, Headers, RequestOptionsArgs, Response, RequestMethod, RequestOptions, Request} from "@angular/http"; | |
import {LoginService} from "../login/login.service"; | |
import {Observable} from "rxjs"; | |
/** | |
* JwtHttpClient is a wrapper over Angular Http. It appends JWT header for authentication. | |
*/ | |
@Injectable() | |
export class JwtHttpClient { | |
constructor(private http: Http, private loginService: LoginService) { | |
} | |
get(url: string, options?: RequestOptionsArgs): Observable<Response> { | |
return this._request(RequestMethod.Get, url, null, options); | |
} | |
post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> { | |
return this._request(RequestMethod.Post, url, body, options); | |
} | |
put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> { | |
return this._request(RequestMethod.Put, url, body, options); | |
} | |
delete(url: string, options?: RequestOptionsArgs): Observable<Response> { | |
return this._request(RequestMethod.Delete, url, null, options); | |
} | |
public patch(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> { | |
return this._request(RequestMethod.Patch, url, body, options); | |
} | |
public head(url: string, options?: RequestOptionsArgs): Observable<Response> { | |
return this._request(RequestMethod.Head, url, null, options); | |
} | |
private _request(method: RequestMethod, url: string, body?: any, options?: RequestOptionsArgs): Observable<Response> { | |
let requestOptions = new RequestOptions(Object.assign({ | |
method: method, | |
url: url, | |
body: body | |
}, options)); | |
if (!requestOptions.headers) { | |
requestOptions.headers = new Headers(); | |
} | |
var isSecureCall: boolean = true; //TODO: url.toLowerCase.startsWith https | |
if (isSecureCall === true) { | |
this.loginService.setAuthorizationHeader(requestOptions.headers); | |
} | |
if(this.loginService.isAccessTokenExpired()) { | |
console.log("Access Token Expired (client side detected)"); | |
return this.loginService.refreshAccessToken() | |
.flatMap((result: Boolean) => { | |
if (result == true) { | |
this.loginService.setAuthorizationHeader(requestOptions.headers); | |
console.log("Re-Performing request after token refresh ... "); | |
return this.performRequest(requestOptions, isSecureCall); | |
} | |
} | |
); | |
} else { | |
return this.performRequest(requestOptions, isSecureCall); | |
} | |
} | |
private performRequest(requestOptions: RequestOptions, isSecureCall: boolean) { | |
return this.http.request(new Request(requestOptions)) | |
.catch(initialError => { | |
if (initialError && initialError.status === 401 && isSecureCall === true) { | |
console.log("Access Token Expired (server side detection"); | |
//token might be expired, attempt refresh token | |
return this.loginService.refreshAccessToken() | |
.flatMap((result: Boolean) => { | |
if (result == true) { | |
this.loginService.setAuthorizationHeader(requestOptions.headers); | |
console.log("Re-Performing request after token refresh (server side detected) ... "); | |
return this.http.request(new Request(requestOptions)); | |
} | |
return Observable.throw(initialError); | |
} | |
); | |
} else { | |
return Observable.throw(initialError); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment