Added authorization

This commit is contained in:
2020-11-29 09:12:02 +05:00
parent 7066d0f233
commit a0dbd1eca8
10 changed files with 44 additions and 22 deletions

View File

@ -2,24 +2,28 @@ const formatTimeLength = (minutes: number) =>
(Math.floor(minutes / 60) > 0 ? `${Math.floor(minutes / 60)} ч. ` : "") +
(minutes % 60 > 0 ? `${minutes % 60} мин.` : "");
const request = async (uri: string, method: "POST" | "GET", body = null) => {
const url = "http://localhost:4000/api/" + uri;
const request = async (uri: string, method: "POST" | "GET", body?) => {
const token = localStorage.getItem("token");
const url = "http://80.240.25.179/api/" + uri;
const headers = {
"Content-Type": "application/json",
method,
body: method == "POST" ? body : undefined,
Authorization: token != "" ? `Token ${token}` : undefined,
};
const options = {
headers,
body: method == "GET" ? body : undefined,
method,
body: JSON.stringify(body),
};
console.log(uri);
try {
if (process.env.NODE_ENV == "development") {
return JSON.parse(`{ "token": "fhjighdfjgjdfigbvhbsdfuyt47" }`);
}
// if (process.env.NODE_ENV == "development") {
// return JSON.parse(`{ "token": "fhjighdfjgjdfigbvhbsdfuyt47" }`);
// }
const res = await fetch(url, options);
return await res.json();
} catch (err) {