session and auth fix

This commit is contained in:
Mateusz Gruszczyński
2026-07-26 15:59:23 +02:00
parent 9a95119b06
commit f145c3c121
6 changed files with 106 additions and 12 deletions
+41 -3
View File
@@ -4,15 +4,19 @@ installGlobalDiagnostics();
import { api } from "@rustpad/api";
import { copyText } from "@rustpad/clipboard";
import { getNickname, getAccessToken, getAuthToken, setAccessToken } from "@rustpad/session";
import { bindIdentityDialog, validateCurrentSession } from "@rustpad/auth-ui";
import { askConfirm } from "@rustpad/modal";
const parts = location.pathname.split("/").filter(Boolean);
const slug = parts[1];
let info;
const shareToken = new URLSearchParams(location.search).get("share");
let accessToken = shareToken || getAuthToken() || getAccessToken("workspace", slug);
let accessToken = shareToken || getAccessToken("workspace", slug);
let nickname = getNickname();
if (shareToken) setAccessToken("workspace", slug, shareToken);
const dialog = document.querySelector("#password-dialog");
const identityDialog = document.querySelector("#identity-dialog");
const workspaceContent = document.querySelector("#workspace-content");
const notesList = document.querySelector("#notes-list");
const notesViewKey = `rustpad:workspace:${slug}:notes-view`;
let notesView = localStorage.getItem(notesViewKey) === "table" ? "table" : "grid";
@@ -127,7 +131,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, access_token: accessToken || 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: nickname || null })
});
location.assign(`${note.url}?view=split&mode=markdown`);
} catch (err) { error.textContent = err.message; }
@@ -155,5 +159,39 @@ document.querySelector("#copy-workspace-link").addEventListener("click", async (
try { await copyText(new URL(location.pathname, location.origin).href); toast("Link copied"); }
catch (e) { toast(e.message); }
});
async function startAuthorizedWorkspace() {
const hadAccountToken = Boolean(getAuthToken());
if (hadAccountToken) {
const session = await validateCurrentSession();
nickname = session?.nickname || getNickname();
}
if (!nickname) {
workspaceContent.hidden = true;
if (!identityDialog.open) identityDialog.showModal();
return;
}
accessToken = shareToken || getAuthToken() || getAccessToken("workspace", slug);
workspaceContent.hidden = false;
await init();
}
bindIdentityDialog({
dialog: identityDialog,
onIdentity: async value => {
nickname = value;
accessToken = shareToken || getAuthToken() || getAccessToken("workspace", slug);
workspaceContent.hidden = false;
await init();
},
});
identityDialog.addEventListener("close", () => {
if (!nickname) queueMicrotask(() => {
workspaceContent.hidden = true;
if (!identityDialog.open) identityDialog.showModal();
});
});
setNotesView(notesView);
init();
startAuthorizedWorkspace();