export async function postData(url, data, token = null) { const elements = url.split("/"); const end = elements[elements.length - 1]; const res = await fetch(end, 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) { alert("Session expired; please log in again."); window.location.href = "login.html"; 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.html"; console.log("Error encountered: " + error); return false; } if (!token) { alert("You are not logged in."); window.location.href = "login.html"; 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; }