UNPKG

ngo-login-client

Version:

Auth and User services for Angular v2 and up. Requires backend REST service.

66 lines 2.71 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import { Injectable, Inject, forwardRef } from '@angular/core'; import { Headers, Http, RequestOptions, XHRBackend } from '@angular/http'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/map'; import 'rxjs/add/operator/catch'; import 'rxjs/add/observable/of'; import { Broadcaster } from 'ngo-base'; var HttpService = /** @class */ (function (_super) { __extends(HttpService, _super); function HttpService(backend, options, broadcaster) { var _this = _super.call(this, backend, options) || this; _this.broadcaster = broadcaster; return _this; } HttpService.prototype.request = function (url, options) { var token = localStorage.getItem('auth_token'); if (token !== null) { if (typeof url === 'string') { if (!options) { // let's make option object options = { headers: new Headers() }; } options.headers.set('Authorization', "Bearer " + token); } else { // we have to add the token to the url object url.headers.set('Authorization', "Bearer " + token); } } return _super.prototype.request.call(this, url, options).catch(this.catchRequestError()); }; HttpService.prototype.catchRequestError = function () { var _this = this; return function (res) { if (res.status === 401 || res.status === 403) { _this.broadcaster.broadcast('authenticationError', res); } else if (res.status === 500) { _this.broadcaster.broadcast('communicationError', res); } return Observable.throw(res); }; }; HttpService.decorators = [ { type: Injectable }, ]; /** @nocollapse */ HttpService.ctorParameters = function () { return [ { type: XHRBackend, }, { type: RequestOptions, }, { type: Broadcaster, decorators: [{ type: Inject, args: [forwardRef(function () { return Broadcaster; }),] },] }, ]; }; return HttpService; }(Http)); export { HttpService }; //# sourceMappingURL=http.service.js.map