fix authorization by token

This commit is contained in:
Daria Golova
2022-06-16 15:25:25 +03:00
parent 632b98a81a
commit 9bef3d9311

View File

@@ -3,10 +3,10 @@ function prepareUrl(url) {
return `/api/${url}`; return `/api/${url}`;
} }
function handleRequest(method, url, attempts, body) { function handleRequest(method, url, attempts, token, body) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
(function internalRequest() { (function internalRequest() {
return request(method, url, body) return request(method, url, token, body)
.then(resolve) .then(resolve)
.catch(err => --attempts > 0 ? internalRequest() : reject(err)) .catch(err => --attempts > 0 ? internalRequest() : reject(err))
})() })()
@@ -20,15 +20,14 @@ function handleRequest(method, url, attempts, body) {
* @param {string} url - endpoint url. * @param {string} url - endpoint url.
* @param {object} body - request body. * @param {object} body - request body.
*/ */
function request(method, url, body) { function request(method, url, token, body) {
let controller = new AbortController; let controller = new AbortController;
let requestOptions = {}; let requestOptions = {};
if (!registrationStore.tokenKey) return Promise.reject();
if (method === 'GET' || method === 'DELETE'){ if (method === 'GET' || method === 'DELETE'){
requestOptions = { requestOptions = {
method: method, method: method,
headers: { headers: {
Authorization: `Token ${registrationStore.tokenKey}`, Authorization: `Token ${token}`,
}, },
signal: controller.signal, signal: controller.signal,
} }
@@ -37,7 +36,7 @@ function request(method, url, body) {
requestOptions = { requestOptions = {
method: method, method: method,
headers: { headers: {
Authorization: `Token ${registrationStore.tokenKey}`, Authorization: `Token ${token}`,
Accept: 'application/json', Accept: 'application/json',
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
@@ -50,19 +49,35 @@ function request(method, url, body) {
}; };
function get(url, attempts = 1) { function get(url, attempts = 1) {
return handleRequest('GET', url, attempts); return handleRequest('GET', url, attempts, null);
}
function getAuth(url, attempts = 1) {
return handleRequest('GET', url, attempts, registrationStore.tokenKey);
} }
function del(url, attempts = 1) { function del(url, attempts = 1) {
return handleRequest('DELETE', url, attempts); return handleRequest('DELETE', url, attempts, null);
}
function delAuth(url, attempts = 1) {
return handleRequest('DELETE', url, attempts, registrationStore.tokenKey);
} }
function post(url, body, attempts = 1) { function post(url, body, attempts = 1) {
return handleRequest('POST', url, attempts, body); return handleRequest('POST', url, attempts, null, body);
}
function postAuth(url, body, attempts = 1) {
return handleRequest('POST', url, attempts, registrationStore.tokenKey, body);
} }
function put(url, body, attempts = 1) { function put(url, body, attempts = 1) {
return handleRequest('PUT', url, attempts, body); return handleRequest('PUT', url, attempts, null, body);
}
function putAuth(url, body, attempts = 1) {
return handleRequest('PUT', url, attempts, registrationStore.tokenKey, body);
} }
export const fetchWrapper = { export const fetchWrapper = {
@@ -70,5 +85,9 @@ export const fetchWrapper = {
del, del,
post, post,
put, put,
getAuth,
delAuth,
postAuth,
putAuth,
baseUrl: '' baseUrl: ''
}; };