pratiks
6/1/2017 - 2:56 AM

From https://www.codementor.io/joshuaaroke/dry-code-vs-wet-code-89xjwv11w

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;
            },
        }
    }