Handling error 401 on all protected routes

This commit is contained in:
Jesús Ángel González 2018-01-25 13:43:47 +01:00
parent 07cffdfbf7
commit e32555353c
3 changed files with 45 additions and 14 deletions

View File

@ -1,6 +1,6 @@
import chrome from 'ui/chrome';
require('ui/modules').get('app/wazuh', []).service('apiReq', function ($q, $http, genericReq, appState) {
require('ui/modules').get('app/wazuh', []).service('apiReq', function ($q, $http, genericReq, appState, $location, $rootScope) {
return {
request: (method, path, body) => {
let defered = $q.defer();
@ -30,7 +30,14 @@ require('ui/modules').get('app/wazuh', []).service('apiReq', function ($q, $http
defered.resolve(data);
}
})
.catch(error => defered.reject(error));
.catch(error => {
if(error.status && error.status === 401){
appState.removeUserCode();
defered.reject(error);
$location.path('/login');
}
defered.reject(error);
});
return defered.promise;
}

View File

@ -1,7 +1,7 @@
const prepError = require('plugins/wazuh/services/prep-error');
import chrome from 'ui/chrome';
require('ui/modules').get('app/wazuh', []).service('genericReq', function ($q, $http) {
require('ui/modules').get('app/wazuh', []).service('genericReq', function ($q, $http, $location, $rootScope, appState) {
const _request = (method, url, payload = null) => {
let defered = $q.defer();
@ -17,7 +17,7 @@ require('ui/modules').get('app/wazuh', []).service('genericReq', function ($q, $
let requestHeaders = { headers: { "Content-Type": 'application/json' }, timeout: 4000 };
let tmpUrl = chrome.addBasePath(url), tmp = null;
if(appState.getUserCode()) requestHeaders.headers.code = appState.getUserCode();
if (method === "GET") tmp = $http.get(tmpUrl, requestHeaders);
if (method === "PUT") tmp = $http.put(tmpUrl, payload, requestHeaders);
if (method === "POST") tmp = $http.post(tmpUrl, payload, requestHeaders);
@ -40,11 +40,13 @@ require('ui/modules').get('app/wazuh', []).service('genericReq', function ($q, $
}
})
.catch(error => {
if(error.status && error.status === -1){
defered.reject({data: 'request_timeout_genericreq', url });
}else if (error.error && error.error !== '0') {
if(error.status && error.status === 401){
appState.removeUserCode();
defered.reject(error);
} else {
$location.path('/login');
} else if(error.status && error.status === -1){
defered.reject({data: 'request_timeout_genericreq', url });
}else {
defered.reject(error);
}
});
@ -66,7 +68,15 @@ require('ui/modules').get('app/wazuh', []).service('genericReq', function ($q, $
_request(method, path, payload)
.then((data) => defered.resolve(data))
.catch(error => defered.reject(prepError(error)));
.catch(error => {
if(error.status && error.status === 401){
appState.removeUserCode();
defered.reject(error);
$location.path('/login');
} else {
defered.reject(prepError(error));
}
});
return defered.promise;
}

View File

@ -1,11 +1,15 @@
import chrome from 'ui/chrome';
require('ui/modules').get('app/wazuh', [])
.service('testAPI', function ($q, $http) {
.service('testAPI', function ($q, $http, $location, $rootScope, appState) {
return {
check_stored: data => {
const headers = {headers:{ "Content-Type": 'application/json' },timeout: 4000};
console.log(appState.getUserCode())
if(appState.getUserCode()) headers.headers.code = appState.getUserCode();
let defered = $q.defer();
console.log(data);
$http
.post(chrome.addBasePath('/api/wazuh-api/checkStoredAPI'), data,{timeout: 4000})
.post(chrome.addBasePath('/api/wazuh-api/checkStoredAPI'), data,headers)
.then(response => {
if (response.error) {
defered.reject(response);
@ -14,7 +18,11 @@ require('ui/modules').get('app/wazuh', [])
}
})
.catch(error => {
if(error.status && error.status === -1){
if(error.status && error.status === 401){
appState.removeUserCode();
defered.reject(error);
$location.path('/login');
} else if(error.status && error.status === -1){
defered.reject({data: 'request_timeout_checkstored'});
} else {
defered.reject(error);
@ -23,10 +31,12 @@ require('ui/modules').get('app/wazuh', [])
return defered.promise;
},
check: data => {
const headers = {headers:{ "Content-Type": 'application/json' },timeout: 4000};
if(appState.getUserCode()) headers.headers.code = appState.getUserCode();
let defered = $q.defer();
const url = chrome.addBasePath("/api/wazuh-api/checkAPI");
$http
.post(url, data, {timeout: 4000})
.post(url, data, headers)
.then(response => {
if (response.error) {
defered.reject(response);
@ -35,7 +45,11 @@ require('ui/modules').get('app/wazuh', [])
}
})
.catch(error => {
if(error.data && error.data.message && error.data.message.includes('ENOTFOUND')) {
if(error.status && error.status === 401){
appState.removeUserCode();
defered.reject(error);
$location.path('/login');
} else if(error.data && error.data.message && error.data.message.includes('ENOTFOUND')) {
defered.reject({data: 'invalid_url'});
} else if(error.data && error.data.message && error.data.message.includes('ECONNREFUSED')) {
defered.reject({data: 'invalid_port'});