-
-
Save yusufola/91cbc01b83c726e96981be889a8f9b49 to your computer and use it in GitHub Desktop.
Interceptor
This file contains 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
(function () { | |
angular.module("amks", ['ui.router', 'restangular', 'aluno']) | |
.run(congiruarApp) | |
.factory('authInterceptor', Interceptador) | |
.config(adicionarInterceptador); | |
congiruarApp.$inject = ['Restangular']; | |
function congiruarApp(restangular) { | |
restangular.setBaseUrl('/api'); | |
restangular.setRestangularFields({ | |
id: '_id' | |
}); | |
} | |
Interceptador.$inject = ["$q", "$window"]; | |
function Interceptador($q, $window) { | |
return { | |
request: function (config) { | |
config.headers = config.headers || {}; | |
if ($window.localStorage.token) { | |
config.headers['Authorization'] = 'Bearer '+$window.localStorage.token; | |
} | |
return config; | |
}, | |
responseError: function (response) { | |
if (response.status === 401) { | |
console.log("Usuário não autenticado."); | |
} | |
toastr.warning(response.data.mensagem); | |
return $q.reject(response.data.mensagem); | |
} | |
}; | |
} | |
adicionarInterceptador.$inject = ["$httpProvider"]; | |
function adicionarInterceptador($httpProvider) { | |
$httpProvider.interceptors.push("authInterceptor"); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment