Signup is limited to the first user by default (UPTIME_SIGNUP). TOTP secrets are sealed with UPTIME_SECRET_KEYS and bound to the user id. Closes #3 Closes #5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
78 lines
2.6 KiB
JavaScript
78 lines
2.6 KiB
JavaScript
(() => {
|
|
const meta = document.querySelector('meta[name="csrf-token"]');
|
|
const api = axios.create({ baseURL: "/api/v1", withCredentials: true });
|
|
api.interceptors.request.use((cfg) => {
|
|
if (meta?.content) cfg.headers["X-CSRF-Token"] = meta.content;
|
|
return cfg;
|
|
});
|
|
window.api = api;
|
|
|
|
const message = (err) =>
|
|
err.response?.data?.message ?? "Verbindung fehlgeschlagen.";
|
|
|
|
const showError = (form, text) => {
|
|
const el = form.querySelector("[data-error]");
|
|
if (!el) return;
|
|
el.textContent = text;
|
|
el.classList.toggle("hidden", !text);
|
|
};
|
|
|
|
document.addEventListener("submit", async (ev) => {
|
|
const form = ev.target.closest("form[data-api]");
|
|
if (!form) return;
|
|
ev.preventDefault();
|
|
const button = form.querySelector("button[type=submit]");
|
|
const body = Object.fromEntries(
|
|
[...new FormData(form)].filter(([k]) => {
|
|
const step = form.querySelector(`[name="${k}"]`)?.closest("[data-step]");
|
|
return !step || !step.hidden;
|
|
}),
|
|
);
|
|
const totpStep = form.querySelector('[data-step="totp"]');
|
|
const url = totpStep && !totpStep.hidden ? `${form.dataset.api}/totp` : form.dataset.api;
|
|
|
|
showError(form, "");
|
|
button && (button.disabled = true);
|
|
try {
|
|
const res = await api.post(url, body);
|
|
if (res.data?.status === "totp_required" && totpStep) {
|
|
form.querySelector('[data-step="password"]').hidden = true;
|
|
totpStep.hidden = false;
|
|
totpStep.querySelector("input")?.focus();
|
|
return;
|
|
}
|
|
location.assign(form.dataset.next || location.href);
|
|
} catch (err) {
|
|
showError(form, message(err));
|
|
} finally {
|
|
button && (button.disabled = false);
|
|
}
|
|
});
|
|
|
|
document.addEventListener("click", async (ev) => {
|
|
if (ev.target.closest("[data-logout]")) {
|
|
await api.post("/auth/logout").catch(() => {});
|
|
location.assign("/login");
|
|
}
|
|
if (ev.target.closest("[data-totp-start]")) {
|
|
const root = ev.target.closest("[data-totp-setup]");
|
|
try {
|
|
const { data } = await api.post("/me/totp/setup");
|
|
root.querySelector("[data-totp-qr]").src = data.qr;
|
|
root.querySelector("[data-totp-secret]").textContent = data.secret;
|
|
root.querySelector("[data-totp-panel]").hidden = false;
|
|
ev.target.hidden = true;
|
|
} catch (err) {
|
|
alert(message(err));
|
|
}
|
|
}
|
|
});
|
|
|
|
document.addEventListener("change", async (ev) => {
|
|
const sel = ev.target.closest("[data-switch-org]");
|
|
if (!sel) return;
|
|
await api.post("/me/org", { org_id: sel.value });
|
|
location.reload();
|
|
});
|
|
})();
|