39 lines
928 B
JavaScript
39 lines
928 B
JavaScript
const api = {
|
|
async post(url, body) {
|
|
const res = await fetch(url, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(body || {}),
|
|
});
|
|
return res.json();
|
|
},
|
|
async put(url, body) {
|
|
const res = await fetch(url, {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(body || {}),
|
|
});
|
|
return res.json();
|
|
},
|
|
async get(url) {
|
|
const res = await fetch(url);
|
|
return res.json();
|
|
},
|
|
};
|
|
|
|
async function refreshNav() {
|
|
const el = document.getElementById("nav-account");
|
|
if (!el) return;
|
|
try {
|
|
const me = await api.get("/api/me");
|
|
if (me && me.username) {
|
|
el.textContent = me.username;
|
|
el.href = "/account";
|
|
return;
|
|
}
|
|
} catch (e) {}
|
|
el.textContent = "Login / Register";
|
|
el.href = "/account";
|
|
}
|
|
window.addEventListener("DOMContentLoaded", refreshNav);
|