function authInterceptor(API, auth) {
return {
// automatically attach Authorization header
request: function (config) {
var token = auth.getToken();
if (config.url.indexOf(API) === 0 && token) {
// using this to keep sending Authorisation token back to the server on every request
config.headers.Authorization = 'Bearer ' + token;
config.headers.token =token;
}
return config;
},
// If a token was sent back, save it
response: function (res) {
if(res.statusCode == 200){
// TODO some success logic
}else if (res.statusCode == 401){
// TODO some unauthorize access logic
}else if(res.statusCode == 404){
// TODO some resource not found access logic
}else if(res.statusCode == 500){
// TODO some server error
}
return res;
},
}
}