kvnallen
5/31/2016 - 10:59 PM

authInterceptor.js

(function () {

    angular.module('moduleName').factory('AuthInterceptor', function ($window, $q, configGeral, $location) {
        return {
            request: function (config) {

                config.headers = config.headers || {};

                var token = $window.localStorage.getItem(configGeral.AUTH_TOKEN);

                if (token)
                    config.headers.Authorization = 'Bearer ' + token;

                return config || $q.when(config);
            },
            responseError: function (rejection) {
                if (rejection.status === 401) {
                    $location.path('urlDeLogout');
                }

                return $q.reject(rejection);
            }
        };
    });
})();