session lifetime

This commit is contained in:
Mateusz Gruszczyński
2026-07-22 23:49:16 +02:00
parent e998a38e49
commit 3ed74b1eac
20 changed files with 262 additions and 74 deletions
+17 -8
View File
@@ -3,13 +3,13 @@ installGlobalDiagnostics();
import { api } from "@rustpad/api";
import { copyText } from "@rustpad/clipboard";
import { getNickname, getPassword, setPassword } from "@rustpad/session";
import { getNickname, getAccessToken, setAccessToken } from "@rustpad/session";
import { askConfirm } from "./modal.js";
const parts = location.pathname.split("/").filter(Boolean);
const slug = parts[1];
let info;
let password = getPassword(slug);
let accessToken = getAccessToken("workspace", slug);
const dialog = document.querySelector("#password-dialog");
const notesList = document.querySelector("#notes-list");
const notesViewKey = `rustpad:workspace:${slug}:notes-view`;
@@ -77,7 +77,7 @@ function renderNotes(notes = notesCache) {
}
async function openWorkspace() {
try {
const data = await api(`/api/workspaces/${encodeURIComponent(slug)}/open`, { method: "POST", body: JSON.stringify({ password: password || null }) });
const data = await api(`/api/workspaces/${encodeURIComponent(slug)}/open`, { method: "POST", body: JSON.stringify({ access_token: accessToken || null }) });
info = data.workspace;
document.querySelector("#workspace-title").textContent = info.title;
document.querySelector("#workspace-url").textContent = location.pathname;
@@ -97,11 +97,20 @@ async function init() {
info = await api(`/api/workspaces/${encodeURIComponent(slug)}`);
document.querySelector("#workspace-title").textContent = info.title;
document.querySelector("#workspace-url").textContent = location.pathname;
if (info.protected && !password) dialog.showModal(); else openWorkspace();
if (info.protected && !accessToken) dialog.showModal(); else openWorkspace();
} catch (e) { document.querySelector("#workspace-error").textContent = e.message; }
}
document.querySelector("#password-form").addEventListener("submit", e => {
e.preventDefault(); password = document.querySelector("#open-password").value; setPassword(slug, password); openWorkspace();
document.querySelector("#password-form").addEventListener("submit", async e => {
e.preventDefault();
try {
const password = document.querySelector("#open-password").value;
const result = await api("/api/access-token", { method: "POST", body: JSON.stringify({ kind: "workspace", slug, password }) });
accessToken = result.access_token;
setAccessToken("workspace", slug, accessToken);
document.querySelector("#open-password").value = "";
document.querySelector("#password-error").textContent = "";
openWorkspace();
} catch (error) { document.querySelector("#password-error").textContent = error.message; }
});
document.querySelector("#new-note-button").addEventListener("click", () => document.querySelector("#note-dialog").showModal());
document.querySelector("#cancel-note").addEventListener("click", () => document.querySelector("#note-dialog").close());
@@ -111,7 +120,7 @@ document.querySelector("#note-form").addEventListener("submit", async e => {
try {
const note = await api(`/api/workspaces/${encodeURIComponent(slug)}/notes`, {
method: "POST",
body: JSON.stringify({ name: document.querySelector("#note-name").value, password: password || null, protect: document.querySelector("#note-protect").checked, created_by: getNickname() || null })
body: JSON.stringify({ name: document.querySelector("#note-name").value, access_token: accessToken || null, protect: document.querySelector("#note-protect").checked, created_by: getNickname() || null })
});
location.assign(`${note.url}?view=split&mode=markdown`);
} catch (err) { error.textContent = err.message; }
@@ -124,7 +133,7 @@ notesList.addEventListener("click", async event => {
button.disabled = true;
try {
await api(`/api/workspaces/${encodeURIComponent(slug)}/notes/${encodeURIComponent(button.dataset.deleteNote)}`, {
method: "DELETE", body: JSON.stringify({ password: password || null })
method: "DELETE", body: JSON.stringify({ access_token: accessToken || null })
});
toast("Note deleted");
await openWorkspace();