added logout api

This commit is contained in:
whoswhip
2025-05-17 22:44:05 -04:00
parent 2a25ab1ab8
commit 704d7149fc
4 changed files with 122 additions and 83 deletions
+2 -2
View File
@@ -26,9 +26,9 @@
<ol class="archive-list" id="archives">
</ol>
<div class="paste-actions">
<button id="previous-button" onclick="previousPage()">&larr; Previous</button>
<button id="previous-button">&larr; Previous</button>
<p id="paste-pages">0/0</p>
<button id="next-button" onclick="nextPage()">Next &rarr;</button>
<button id="next-button">Next &rarr;</button>
</div>
</div>
+77 -78
View File
@@ -1,67 +1,82 @@
let pages = 0;
let currentPage = 0;
let limit = 25;
document.addEventListener('DOMContentLoaded', async () => {
const queries = new URLSearchParams(window.location.search);
let page = parseInt(queries.get("page"));
let limit = parseInt(queries.get("limit"));
if (isNaN(page)) {
page = 0;
}
if (page < 0) {
page = 0;
}
if (isNaN(limit) || limit < 1 || limit > 25) {
limit = 25;
}
currentPage = page;
async function renderArchive(page, limit) {
const archiveList = document.getElementById("archives");
const pagesElement = document.getElementById("paste-pages");
const previousButton = document.getElementById("previous-button");
const nextButton = document.getElementById("next-button");
let loadingOverlay = document.getElementById("archive-loading-overlay");
if (!loadingOverlay) {
loadingOverlay = document.createElement("div");
loadingOverlay.id = "archive-loading-overlay";
loadingOverlay.innerHTML = '<div class="loading-spinner"><div></div><div></div><div></div></div>';
loadingOverlay.style.position = "absolute";
loadingOverlay.style.top = 0;
loadingOverlay.style.left = 0;
loadingOverlay.style.width = "100%";
loadingOverlay.style.height = "100%";
loadingOverlay.style.display = "flex";
loadingOverlay.style.justifyContent = "center";
loadingOverlay.style.alignItems = "center";
loadingOverlay.style.background = "transparent";
loadingOverlay.style.zIndex = 10;
archiveList.parentElement.style.position = "relative";
archiveList.parentElement.appendChild(loadingOverlay);
} else {
loadingOverlay.style.display = "flex";
}
try {
archiveList.innerHTML = '<div class="loading-spinner"><div></div><div></div><div></div></div>';
const archiveData = await getPastes(page, limit);
const pastes = archiveData.pastes;
pages = archiveData.pages;
pagesElement.innerText = `${parseInt(page) + 1}/${pages}`;
currentPage = page;
pagesElement.innerText = `${page + 1}/${pages}`;
previousButton.disabled = page <= 0;
nextButton.disabled = page + 1 >= pages;
if (pastes.length === 0) {
archiveList.innerHTML = '<div class="no-pastes">No pastes found</div>';
return;
}
let archiveListHTML = "";
pastes.forEach(paste => {
const title = paste.title.length > 30 ? paste.title.substring(0, 30) + '...' : paste.title;
archiveListHTML += `
<li>
<a href="/${paste.id}">
<p id="archive-title" title="${paste.title}">${title}</p>
<p id="archive-date">${formatDate(paste.created)}</p>
<p id="archive-syntax">${paste.syntax || 'Plain Text'}</p>
</a>
</li>
`;
});
if (pastes.length === 0) {
archiveListHTML = '<div class="no-pastes">No pastes found</div>';
} else {
pastes.forEach(paste => {
const title = paste.title.length > 30 ? paste.title.substring(0, 30) + '...' : paste.title;
archiveListHTML += `
<li>
<a href="/${paste.id}">
<p id="archive-title" title="${paste.title}">${title}</p>
<p id="archive-date">${formatDate(paste.created)}</p>
<p id="archive-syntax">${paste.syntax || 'Plain Text'}</p>
</a>
</li>
`;
});
}
archiveList.innerHTML = archiveListHTML;
} catch (error) {
console.error('Error fetching pastes:', error);
archiveList.innerHTML = '<div class="error-message">Failed to load pastes. Please try again later.</div>';
} finally {
if (loadingOverlay) loadingOverlay.style.display = "none";
}
}
document.addEventListener('DOMContentLoaded', async () => {
const queries = new URLSearchParams(window.location.search);
let page = parseInt(queries.get("page"));
limit = parseInt(queries.get("limit"));
if (isNaN(page) || page < 0) page = 0;
if (isNaN(limit) || limit < 1 || limit > 25) limit = 25;
await renderArchive(page, limit);
document.getElementById("previous-button").addEventListener("click", previousPage);
document.getElementById("next-button").addEventListener("click", nextPage);
});
async function getPastes(page, limit) {
@@ -95,43 +110,27 @@ function formatDate(unix) {
}
function nextPage() {
const queries = new URLSearchParams(window.location.search);
let page = parseInt(queries.get("page"));
let limit = parseInt(queries.get("limit"));
if (isNaN(page) || page < 0) {
page = 0;
}
if (isNaN(limit) || limit < 1 || limit > 25) {
limit = 25;
}
if (page + 1 > pages) {
return;
}
page++;
window.location.href = `/archive?page=${page}&limit=${limit}`;
if (currentPage + 1 >= pages) return;
currentPage++;
updateUrlAndRender();
}
function previousPage() {
const queries = new URLSearchParams(window.location.search);
let page = parseInt(queries.get("page"));
let limit = parseInt(queries.get("limit"));
if (currentPage <= 0) return;
currentPage--;
updateUrlAndRender();
}
if (isNaN(page) || page < 0) {
page = 0;
}
if (isNaN(limit) || limit < 1 || limit > 25) {
limit = 25;
}
function updateUrlAndRender() {
const url = `/archive?page=${currentPage}&limit=${limit}`;
window.history.pushState({page: currentPage, limit: limit}, '', url);
renderArchive(currentPage, limit);
}
if (page <= 0) {
return;
window.onpopstate = function(event) {
if (event.state) {
currentPage = event.state.page;
limit = event.state.limit;
renderArchive(currentPage, limit);
}
page--;
window.location.href = `/archive?page=${page}&limit=${limit}`;
}
};
+3 -3
View File
@@ -139,7 +139,7 @@ function displayPastes() {
const pasteLink = document.createElement('a');
pasteLink.href = `/paste/${paste.id}`;
pasteLink.href = `/${paste.id}`;
pasteLink.style.display = 'grid';
pasteLink.style.gridTemplateColumns = '2fr 1fr 1fr';
@@ -212,7 +212,7 @@ function logout() {
document.getElementById('confirm-logout').onclick = async function () {
try {
await fetch('/api/accounts/logout', { method: 'POST', credentials: 'include' });
await fetch('/api/accounts/logout', { method: 'POST'});
} catch { }
window.location.href = '/login';
};
@@ -264,7 +264,7 @@ function deleteAccount() {
confirmBtn.onclick = async function () {
if (confirmBtn.disabled) return;
try {
await fetch('/api/accounts/delete', { method: 'POST', credentials: 'include' });
await fetch('/api/accounts/delete', { method: 'POST'});
} catch { }
window.location.href = '/register';
};