fix: more login/session issues addressed

This commit is contained in:
Andy Burke 2025-07-04 15:16:51 -07:00
parent cf46450f5f
commit ee152a514c
8 changed files with 85 additions and 51 deletions

View file

@ -11,8 +11,8 @@ const api = {
const headers = {
Accept: "application/json",
"x-session_id": session_id,
"x-totp": await otp_totp(session_secret),
"x-session_id": session_id ?? "",
"x-totp": session_secret ? await otp_totp(session_secret) : "",
...(options.headers ?? {}),
};
@ -26,7 +26,7 @@ const api = {
fetch_options.body = JSON.stringify(options.json);
}
const response = await fetch(`/api${url}`, fetch_options);
const response = await fetch(url, fetch_options);
return response;
},

View file

@ -7,11 +7,15 @@ document.addEventListener("DOMContentLoaded", () => {
form.onsubmit = async (event) => {
event.preventDefault();
const url = form.action;
const method = form.dataset.method ?? "POST";
const json = {};
const form_data = new FormData(form);
const body = {};
for (const [key, value] of form_data.entries()) {
const elements = key.split(".");
let current = body;
let current = json;
for (const element of elements.slice(0, elements.length - 1)) {
current[element] = current[element] ?? {};
current = current[element];
@ -20,12 +24,10 @@ document.addEventListener("DOMContentLoaded", () => {
current[elements.slice(elements.length - 1).shift()] = value;
}
const url = form.action;
const method = form.dataset.method ?? "POST";
console.dir({
method,
form,
json,
});
try {
// TODO: send session header
@ -37,11 +39,10 @@ document.addEventListener("DOMContentLoaded", () => {
};
if (["POST", "PUT", "PATCH"].includes(method)) {
options.headers["Content-Type"] = "application/json";
options.body = JSON.stringify(body);
options.json = json;
}
const response = await fetch(url, options);
const response = await api.fetch(url, options);
if (!response.ok) {
const error_body = await response.json();