- force user redirect to login page after response 403;

This commit is contained in:
Vitalii Kiiko
2024-12-04 17:34:33 +01:00
parent 5f02a89f1d
commit 0142e20ede

View File

@@ -1,5 +1,5 @@
import { storeGet } from '../store'; import { storeGet, storeSet } from '../store';
import * as Sentry from "@sentry/browser"; import * as Sentry from '@sentry/browser';
const LOCAL_DEVELOPMENT = process.env.REACT_APP_LOCAL_DEVELOPMENT; const LOCAL_DEVELOPMENT = process.env.REACT_APP_LOCAL_DEVELOPMENT;
@@ -7,13 +7,13 @@ export class NetworkService {
static TOKEN_KEY static TOKEN_KEY
static REFRESH_TOKEN_KEY static REFRESH_TOKEN_KEY
static logApiError = (endpoint, status = 0, resp) => { static logApiError = (endpoint, status = 0, resp = {}) => {
try { if (status === 500) {
if ([500].includes(status)) {
if (LOCAL_DEVELOPMENT !== '1') { if (LOCAL_DEVELOPMENT !== '1') {
try {
Sentry.init({ Sentry.init({
dsn: "https://e7b2134f7d816f663bb83e51b106a694@o4508381921738752.ingest.de.sentry.io/4508381935501392", dsn: 'https://e7b2134f7d816f663bb83e51b106a694@o4508381921738752.ingest.de.sentry.io/4508381935501392',
environment: process.env.NODE_ENV || "development" environment: process.env.NODE_ENV || 'development'
}); });
const error = new Error(`Status ${status}`); const error = new Error(`Status ${status}`);
@@ -24,52 +24,18 @@ export class NetworkService {
details: resp details: resp
} }
}); });
}
}
} catch (err) { } catch (err) {
console.log(err); console.log(err);
} }
} }
} else if (status === 403) {
static postEmptyResponse = (url, body, callback, errorCallback) => { storeSet.main.token('');
fetch(url, { const { pathname } = window.location;
method: 'POST', if (!['/login', '/loginadmin', '/reset-password'].includes(pathname)) {
mode: 'cors', window.location.replace('/login');
headers: { }
'Content-Type': 'application/json', }
'Authorization': storeGet.main.getToken(), }
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify(body)
})
.then(data => {
if (data.status >= 400 && data.status <= 599)
errorCallback(data.status)
else
callback()
})
.catch(err => errorCallback(err));
};
static putEmptyResponse = (url, body, callback, errorCallback) => {
fetch(url, {
method: 'PUT',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
'Authorization': storeGet.main.getToken(),
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify(body)
})
.then(data => {
if (data.status >= 400 && data.status <= 599)
errorCallback(data.status)
else
callback()
})
.catch(err => errorCallback(err));
};
static post = (url, body, callback, errorCallback, queryParams) => { static post = (url, body, callback, errorCallback, queryParams) => {
@@ -101,6 +67,7 @@ export class NetworkService {
}) })
.then(async response => { .then(async response => {
let status = response.status; let status = response.status;
this.logApiError(url, status);
return { response: await response.json(), status: status } return { response: await response.json(), status: status }
}) })
.then(data => { .then(data => {
@@ -144,6 +111,7 @@ export class NetworkService {
}) })
.then(async response => { .then(async response => {
let status = response.status; let status = response.status;
this.logApiError(url, status);
return { response: await response.json(), status: status } return { response: await response.json(), status: status }
}) })
.then(data => { .then(data => {
@@ -187,6 +155,7 @@ export class NetworkService {
}) })
.then(async response => { .then(async response => {
let status = response.status; let status = response.status;
this.logApiError(url, status);
return { response: await response.blob(), status: status } return { response: await response.blob(), status: status }
}) })
.then(data => { .then(data => {
@@ -228,6 +197,7 @@ export class NetworkService {
}) })
.then(async response => { .then(async response => {
let status = response.status; let status = response.status;
this.logApiError(url, status);
return { response: await response.json(), status: status } return { response: await response.json(), status: status }
}) })
.then(data => { .then(data => {
@@ -241,28 +211,6 @@ export class NetworkService {
.catch(err => errorCallback(err)); .catch(err => errorCallback(err));
}; };
static patch = (url, body, callback, errorCallback) => {
fetch(url, {
method: 'PATCH',
mode: 'cors',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
})
.then(async response => {
let status = response.status;
return { response: await response.json(), status: status }
})
.then(data => {
if (data.status >= 400 && data.status <= 599)
errorCallback(data.response)
else
callback(data.response)
})
.catch(err => errorCallback(err));
};
static put = (url, body, callback, errorCallback, queryParams = null) => { static put = (url, body, callback, errorCallback, queryParams = null) => {
if (queryParams) { if (queryParams) {
url += '?' url += '?'
@@ -293,6 +241,7 @@ export class NetworkService {
}) })
.then(async response => { .then(async response => {
let status = response.status; let status = response.status;
this.logApiError(url, status);
return { response: await response.json(), status: status } return { response: await response.json(), status: status }
}) })
.then(data => { .then(data => {
@@ -306,79 +255,6 @@ export class NetworkService {
.catch(err => errorCallback(err)); .catch(err => errorCallback(err));
}; };
static unauthorizedPostEmptyResponse = (url, body, callback, errorCallback) => {
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify(body)
})
.then(data => callback(data))
.catch(err => errorCallback(err));
};
static unauthorizedPutEmptyResponse = (url, body, callback, errorCallback) => {
fetch(url, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify(body)
})
.then(data => callback(data))
.catch(err => errorCallback(err));
};
static unauthorizedGet = (url, queryParams, callback, errorCallback) => {
fetch(url, {
method: 'GET',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
}
})
.then(async response => {
let status = response.status;
return { response: await response.json(), status: status }
})
.then(data => {
if (data.status >= 400 && data.status <= 599)
errorCallback(data.response)
else
callback(data.response)
})
.catch(err => errorCallback(err));
};
static unauthorizedPatch = (url, body, callback, errorCallback) => {
fetch(url, {
method: 'PATCH',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body)
})
.then(async response => {
let status = response.status;
return { response: await response.json(), status: status }
})
.then(data => {
if (data.status >= 400 && data.status <= 599)
errorCallback(data.response)
else
callback(data.response)
})
.catch(err => errorCallback(err));
};
static isNotBlank(value) { static isNotBlank(value) {
return value !== null && value !== undefined && value !== '' return value !== null && value !== undefined && value !== ''
} }
@@ -413,7 +289,7 @@ export class NetworkService {
}) })
.then(async response => { .then(async response => {
let status = response.status; let status = response.status;
//console.log('status in fetch:', status) this.logApiError(url, status);
return { response: await response.json(), status: status } return { response: await response.json(), status: status }
}) })
.then(data => { .then(data => {
@@ -459,6 +335,7 @@ export class NetworkService {
}) })
.then(async response => { .then(async response => {
let status = response.status; let status = response.status;
this.logApiError(url, status);
return { response: await response.blob(), status: status } return { response: await response.blob(), status: status }
}) })
.then(data => { .then(data => {
@@ -472,53 +349,6 @@ export class NetworkService {
.catch(err => errorCallback(err)); .catch(err => errorCallback(err));
}; };
static promiseGet = async (url, queryParams = null) => {
const response = await fetch(url, {
method: 'GET',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + storeGet.main.getToken(),
'Access-Control-Allow-Origin': '*'
}
});
const json = await response.json();
return json;
}
static deleteEmptyResponse = (url, callback, errorCallback, queryParams = null) => {
if (queryParams) {
let params = '?'
for (let i = 0; i < queryParams.length; i++) {
params += queryParams[i][0] + '=' + queryParams[i][1]
if (queryParams.length !== i + 1)
params += '&'
url += params
params = ''
}
}
fetch(url, {
method: 'DELETE',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
'Authorization': storeGet.main.getToken(),
'Access-Control-Allow-Origin': '*'
}
})
.then(data => {
if (data.status >= 400 && data.status <= 599)
errorCallback(data.status)
else
callback()
})
.catch(err => errorCallback(err));
}
static delete = (url, body, callback, errorCallback, queryParams = null) => { static delete = (url, body, callback, errorCallback, queryParams = null) => {
if (queryParams) { if (queryParams) {
let params = '?' let params = '?'
@@ -543,6 +373,7 @@ export class NetworkService {
}) })
.then(async response => { .then(async response => {
let status = response.status; let status = response.status;
this.logApiError(url, status);
return { response: await response.json(), status: status } return { response: await response.json(), status: status }
}) })
.then(data => { .then(data => {