From 704d7149fc772369d33b4305f5ba1a610b30ac78 Mon Sep 17 00:00:00 2001 From: whoswhip <124531971+whoswhip@users.noreply.github.com> Date: Sat, 17 May 2025 22:44:05 -0400 Subject: [PATCH] added logout api --- SharpbinV2.Server/Program.cs | 40 ++++++ SharpbinV2.Server/wwwroot/archive.html | 4 +- SharpbinV2.Server/wwwroot/js/archive.js | 155 ++++++++++++------------ SharpbinV2.Server/wwwroot/js/dash.js | 6 +- 4 files changed, 122 insertions(+), 83 deletions(-) diff --git a/SharpbinV2.Server/Program.cs b/SharpbinV2.Server/Program.cs index 1825454..c0a6437 100644 --- a/SharpbinV2.Server/Program.cs +++ b/SharpbinV2.Server/Program.cs @@ -550,6 +550,46 @@ namespace SharpbinV2.Server await context.Response.WriteAsJsonAsync(new { success = true, message = "Account deleted." }); }); + app.MapPost("/api/accounts/logout", async (HttpContext context) => + { + var requestdetails = GetRequestDetails(context); + if (string.IsNullOrEmpty(requestdetails.Token)) + { + context.Response.StatusCode = 400; + await context.Response.WriteAsJsonAsync(new { success = false, message = "No token provided, not authorized." }); + return; + } + var user = await Database.UserFromToken(requestdetails.Token); + if (user == null) + { + context.Response.StatusCode = 400; + await context.Response.WriteAsJsonAsync(new { success = false, message = "Invalid token." }); + return; + } + using (var connection = new SqliteConnection(MainDatabaseConnection)) + { + await connection.OpenAsync(); + using (var command = connection.CreateCommand()) + { + command.CommandText = "DELETE FROM sessions WHERE Token = @Token;"; + command.Parameters.AddWithValue("@Token", requestdetails.Token); + await command.ExecuteNonQueryAsync(); + } + await connection.CloseAsync(); + } + + var cookieOptions = new CookieOptions + { + HttpOnly = true, + Secure = true, + SameSite = SameSiteMode.Strict, + Expires = DateTimeOffset.UtcNow.AddDays(-1) + }; + context.Response.Cookies.Append("Authorization", "", cookieOptions); + context.Response.StatusCode = 200; + await context.Response.WriteAsJsonAsync(new { success = true, message = "Logged out." }); + }); + app.MapPost("/api/pastes/create", async (HttpContext context) => { diff --git a/SharpbinV2.Server/wwwroot/archive.html b/SharpbinV2.Server/wwwroot/archive.html index fc1e4ec..07d31f9 100644 --- a/SharpbinV2.Server/wwwroot/archive.html +++ b/SharpbinV2.Server/wwwroot/archive.html @@ -26,9 +26,9 @@
- +

0/0

- +
diff --git a/SharpbinV2.Server/wwwroot/js/archive.js b/SharpbinV2.Server/wwwroot/js/archive.js index 12c0dab..9b1e82c 100644 --- a/SharpbinV2.Server/wwwroot/js/archive.js +++ b/SharpbinV2.Server/wwwroot/js/archive.js @@ -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 = '
'; + 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 = '
'; - 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 = '
No pastes found
'; - return; - } - let archiveListHTML = ""; - pastes.forEach(paste => { - const title = paste.title.length > 30 ? paste.title.substring(0, 30) + '...' : paste.title; - archiveListHTML += ` -
  • - -

    ${title}

    -

    ${formatDate(paste.created)}

    -

    ${paste.syntax || 'Plain Text'}

    -
    -
  • - `; - }); - + if (pastes.length === 0) { + archiveListHTML = '
    No pastes found
    '; + } else { + pastes.forEach(paste => { + const title = paste.title.length > 30 ? paste.title.substring(0, 30) + '...' : paste.title; + archiveListHTML += ` +
  • + +

    ${title}

    +

    ${formatDate(paste.created)}

    +

    ${paste.syntax || 'Plain Text'}

    +
    +
  • + `; + }); + } archiveList.innerHTML = archiveListHTML; } catch (error) { console.error('Error fetching pastes:', error); archiveList.innerHTML = '
    Failed to load pastes. Please try again later.
    '; + } 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}`; -} \ No newline at end of file +}; \ No newline at end of file diff --git a/SharpbinV2.Server/wwwroot/js/dash.js b/SharpbinV2.Server/wwwroot/js/dash.js index 7ae5b6e..4b8d4ac 100644 --- a/SharpbinV2.Server/wwwroot/js/dash.js +++ b/SharpbinV2.Server/wwwroot/js/dash.js @@ -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'; };