session and auth fix
This commit is contained in:
+41
-3
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user