Files
PasswordManager/public/client.js
RochesterX 9e88712e9f Search
2025-11-18 21:16:35 -05:00

52 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) {
alert("Session expired; please log in again.");
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;
}