fix: more login/session issues addressed
This commit is contained in:
parent
cf46450f5f
commit
ee152a514c
8 changed files with 85 additions and 51 deletions
|
@ -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;
|
||||
},
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue