-
-
Save anderson-mota/8603c9f4d5454e35cde3 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