51 lines
1.3 KiB
JavaScript
51 lines
1.3 KiB
JavaScript
export async function postData(url, data, token = null) {
|
|
console.log("fetching here: " + url);
|
|
const res = await fetch(url, token == null ? {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(data)
|
|
} : {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"Authorization" : "Bearer " + token
|
|
},
|
|
body: JSON.stringify(data)
|
|
});
|
|
|
|
const resultObject = await res.json();
|
|
|
|
if (resultObject.logout === true) {
|
|
window.location.href = "/login";
|
|
return resultObject;
|
|
}
|
|
|
|
return resultObject;
|
|
}
|
|
|
|
export async function verifyLogin() {
|
|
let token = null;
|
|
try {
|
|
token = JSON.stringify(localStorage.getItem("token"));
|
|
} catch (error) {
|
|
alert("You are not logged in.");
|
|
window.location.href = "login";
|
|
console.log("Error encountered: " + error);
|
|
return false;
|
|
}
|
|
if (!token) {
|
|
alert("You are not logged in.");
|
|
window.location.href = "/login";
|
|
console.log("Token was null");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
export async function verifyRole(role) {
|
|
if (!verifyLogin()) return;
|
|
|
|
return postData("/getInfo", {}, localStorage.getItem("token")).Role === role;
|
|
}
|