all files / src/interceptor/ ngJwtAuthInterceptor.ts

100% Statements 29/29
100% Branches 10/10
100% Functions 5/5
100% Lines 29/29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48  52× 52× 11× 10×   11×   52× 30× 30×     29×   52×         52× 52×                  
"use strict";
exports.authorizationUpdateHeader = 'Authorization-Update';
var NgJwtAuthInterceptor = (function () {
    function NgJwtAuthInterceptor(_$q, _$injector) {
        var _this = this;
        this.getNgJwtAuthService = function () {
            if (_this.ngJwtAuthService == null) {
                _this.ngJwtAuthService = _this.$injector.get('ngJwtAuthService');
            }
            return _this.ngJwtAuthService;
        };
        this.response = function (response) {
            var updateHeader = response.headers(exports.authorizationUpdateHeader);
            if (updateHeader) {
                var newToken = updateHeader.replace('Bearer ', '');
                var ngJwtAuthService = _this.getNgJwtAuthService();
                if (!ngJwtAuthService.validateToken(newToken)) {
                    return response; //if it is not a valid JWT, just return the response as it might be some other kind of token that is being updated.
                }
                ngJwtAuthService.processNewToken(newToken);
            }
            return response;
        };
        this.responseError = function (rejection) {
            var ngJwtAuthService = _this.getNgJwtAuthService();
            //if the response is on a login method, reject immediately
            if (ngJwtAuthService.isLoginMethod(rejection.config.url)) {
                return _this.$q.reject(rejection);
            }
            if (401 === rejection.status) {
                return ngJwtAuthService.handleInterceptedUnauthorisedResponse(rejection);
            }
            return _this.$q.reject(rejection);
        };
        this.$q = _$q;
        this.$injector = _$injector;
    }
    /**
     * Construct the service with dependencies injected
     * @param _$q
     * @param _$injector
     */
    NgJwtAuthInterceptor.$inject = ['$q', '$injector'];
    return NgJwtAuthInterceptor;
}());
exports.NgJwtAuthInterceptor = NgJwtAuthInterceptor;