Created
March 15, 2016 20:19
-
-
Save danjesus/7a8f0f72a66f9165958c to your computer and use it in GitHub Desktop.
A simple angular 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 () { | |
'use strict'; | |
angular.module('smartticket.services') | |
.factory('BearerAuthInterceptor', function ($window, $q, $localStorage, $location) { | |
return { | |
request: function (config) { | |
config.headers = config.headers || {}; | |
var token = $localStorage.get('token'); | |
if (token) { | |
config.headers.Authorization = 'Bearer ' + token; | |
} | |
return config || $q.when(config); | |
}, | |
response: function (response) { | |
if (response.status === 401) { | |
$location.path('login'); | |
} | |
return response || $q.when(response); | |
} | |
}; | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment