Created
January 23, 2015 00:57
-
-
Save geovanisouza92/53d350393a0a21507735 to your computer and use it in GitHub Desktop.
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
angular.module('app') | |
.config(configFn) | |
.factory('AuthInterceptor', AuthInterceptor); | |
/* @ngInject */ | |
function configFn($httpProvider) { | |
$httpProvider.interceptors.push('AuthInterceptor'); | |
} | |
/* @ngInject */ | |
function AuthInterceptor($rootScope, $q, secureStore) { | |
var interceptor = { | |
request: requestFn, | |
response: responseFn, | |
}; | |
function requestFn() { | |
config.headers = config.headers || {}; | |
var token = secureStore.get('token'); | |
if (!!token) { // token exists? | |
config.headers.Authorization = 'Bearer ' + token; | |
} | |
return config; | |
} | |
function responseFn() { | |
if (response.status === 401) { | |
console.log('401 Unauthorized'); | |
} | |
return response || $q.when(response); | |
} | |
return interceptor; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment