(function () {
    'use strict';

    angular
        .module('app')
        .factory('AuthHttpResponseInterceptor', AuthHttpResponseInterceptor);

    AuthHttpResponseInterceptor.$inject = ['$q', '$location'];

    function AuthHttpResponseInterceptor($q, $location) {
        return {
            response: function (response) {
                if (response.status === 401) {
                    console.log("Response 401");
                }
                return response || $q.when(response);
            },
            responseError: function (rejection) {
                if (rejection.status === 401) {
                    $location.path('/login').search('returnUrl', $location.path());
                }
                return $q.reject(rejection);
            }
        }
    }
})();
