- initial;

This commit is contained in:
Vitalii Kiiko
2024-08-09 08:51:20 +02:00
commit 987b1b0110
46 changed files with 30261 additions and 0 deletions

View File

@@ -0,0 +1,79 @@
import { NetworkService } from './network-service';
import { jwtDecode } from 'jwt-decode';
// store
import { storeGet } from '../store';
const API_BASE_URL = process.env.REACT_APP_API_EXECUTION_ADDRESS;
export default class AuthenticationService {
static wasLoggedIn = () => {
const token = storeGet.main.getToken();
return token ?? false;
};
static isExpired = () => {
const token = storeGet.main.getToken();
if (!token) {
return false;
}
let decoded = jwtDecode(token);
if (!decoded) {
return false;
}
let currentTs = new Date().getTime();
if (currentTs >= (decoded.exp * 1000)) {
return true; //FIXME: try refresh
}
return false;
};
static isLoggedIn = () => {
const token = storeGet.main.getToken();
if (!token) {
return false;
}
let decoded = jwtDecode(token);
if (!decoded) {
return false;
}
let currentTs = new Date().getTime();
if (currentTs >= decoded.exp * 1000) {
return false; //FIXME: try refresh
}
return true;
};
static login = (loginRequest, callback, errCallback) => {
NetworkService.unauthorizedPost(`${API_BASE_URL}/user/login/`, loginRequest, callback, errCallback);
};
static registerUser = (registerRequest, callback, errCallback) => {
NetworkService.post(`${API_BASE_URL}/user/register/`, registerRequest, callback, errCallback);
};
static forgotPassword = (request, callback, errCallback) => {
NetworkService.unauthorizedPost(`${API_BASE_URL}/user/reset_password_request/?email=` + request, {}, callback, errCallback);
}
static checkTokenForgotPassword = (request, callback, errCallback) => {
NetworkService.unauthorizedGet(`${API_BASE_URL}/user/reset_token_check/?token=` + request, {}, callback, errCallback);
}
static changePassword = (request, callback, errCallback) => {
NetworkService.unauthorizedPatch(`${API_BASE_URL}/user/reset_password/`, request, callback, errCallback);
}
}

View File

@@ -0,0 +1,374 @@
import { storeGet } from '../store';
export class NetworkService {
static TOKEN_KEY
static REFRESH_TOKEN_KEY
static postEmptyResponse = (url, body, callback, errorCallback) => {
fetch(url, {
method: 'POST',
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 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) => {
if (queryParams) {
url += '?'
for (let i = 0; i < queryParams.length; i++) {
if (queryParams[i] && this.isNotBlank(queryParams[i][0]) && this.isNotBlank(queryParams[i][1])) {
let param = queryParams[i][0] + '=' + queryParams[i][1]
if (i !== queryParams.length - 1)
param += '&'
url += param;
}
}
if (url.charAt(url.length) === '&')
url = url.substring(0, url.length - 1);
}
fetch(url, {
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + storeGet.main.getToken(),
},
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 unauthorizedPost = (url, body, callback, errorCallback) => {
fetch(url, {
method: 'POST',
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 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) => {
if (queryParams) {
url += '?'
for (let i = 0; i < queryParams.length; i++) {
if (queryParams[i] && this.isNotBlank(queryParams[i][0]) && this.isNotBlank(queryParams[i][1])) {
let param = queryParams[i][0] + '=' + queryParams[i][1]
if (i !== queryParams.length - 1)
param += '&'
url += param;
}
}
if (url.charAt(url.length) === '&')
url = url.substring(0, url.length - 1);
}
fetch(url, {
method: 'PUT',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + storeGet.main.getToken(),
},
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 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) {
return value !== null && value !== undefined && value !== ''
}
static get = (url, callback, errorCallback, queryParams = null) => {
if (queryParams) {
url += '?'
for (let i = 0; i < queryParams.length; i++) {
if (queryParams[i] && this.isNotBlank(queryParams[i][0]) && this.isNotBlank(queryParams[i][1])) {
let param = queryParams[i][0] + '=' + queryParams[i][1]
if (i !== queryParams.length - 1)
param += '&'
url += param;
}
}
if (url.charAt(url.length) === '&')
url = url.substring(0, url.length - 1);
}
fetch(url, {
method: 'GET',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + storeGet.main.getToken(),
}
})
.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 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) => {
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': 'Bearer ' + storeGet.main.getToken(),
},
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));
};
}