This commit is contained in:
maurice committed 2026-11-16 14:47:10 +01:00
1 parent 3d9777d81a
commit 084f78c60e
17 files changed
+253 -237

No files matched your search

+28 -16
View File
@@ -192,16 +192,27 @@ function reference(value, url) {
let id = value.trim();
if (/^(https?:\/\/|\/)/i.test(id)) {
const parsed = new URL(id, location.origin);
if (parsed.origin !== location.origin) throw new Error("Use an ID or a link from this Pastable server.");
if (parsed.origin !== location.origin) throw new Error("Use an ID or a link from this Pastabble server.");
url = parsed.pathname.startsWith("/to/");
id = decodeURIComponent(parsed.pathname.slice(url ? 4 : 1));
}
if (!id || id.includes("/") || ["gui", "list", "once", "to", "static"].includes(id)) {
throw new Error("Enter a valid share ID or a full Pastable share URL.");
if (!id || id.includes("/") || ["docs", "list", "once", "to", "static"].includes(id)) {
throw new Error("Enter a valid share ID or a full Pastabble share URL.");
}
return { id, url };
}
function setSession(username, auth, result = []) {
if (authorization !== auth) resetEditor();
authorization = auth;
notes = result;
$("username").value = username;
$("session-label").textContent = username || "Anonymous";
$("logout").hidden = !auth;
renderNotes();
updateEditor();
}
$("login-form").addEventListener("submit", event => {
event.preventDefault();
busy(event.submitter, async () => {
@@ -209,27 +220,18 @@ $("login-form").addEventListener("submit", event => {
const credentials = new TextEncoder().encode(`${username}:${$("password").value}`);
const auth = `Basic ${btoa(Array.from(credentials, byte => String.fromCharCode(byte)).join(""))}`;
const result = await api("/list", { accept: "application/json" }, auth);
if (authorization && authorization !== auth) resetEditor();
authorization = auth;
notes = result;
localStorage.setItem("pastable.session", JSON.stringify({ username, authorization: auth }));
setSession(username, auth, result);
$("password").value = "";
$("session-label").textContent = username;
$("logout").hidden = false;
$("account").open = false;
renderNotes();
updateEditor();
notify(`Signed in as ${username}. New shares will belong to your account.`);
});
});
$("logout").addEventListener("click", () => {
authorization = null;
notes = [];
localStorage.removeItem("pastable.session");
$("login-form").reset();
$("logout").hidden = true;
$("session-label").textContent = "Anonymous";
resetEditor();
renderNotes();
setSession("", null);
notify("Signed out.");
});
@@ -305,3 +307,13 @@ window.addEventListener("hashchange", () => showPanel(location.hash.slice(1)));
showPanel(location.hash.slice(1));
renderNotes();
updateEditor();
try {
const session = JSON.parse(localStorage.getItem("pastable.session"));
if (session && typeof session.username === "string" && typeof session.authorization === "string") {
setSession(session.username, session.authorization);
loadNotes().catch(error => notify(error.message, true));
}
} catch {
notify("Could not restore your saved sign-in. Please sign in again.", true);
}