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
+40
View File
@@ -550,6 +550,46 @@ namespace SharpbinV2.Server
await context.Response.WriteAsJsonAsync(new { success = true, message = "Account deleted." }); 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) => app.MapPost("/api/pastes/create", async (HttpContext context) =>
{ {
+2 -2
View File
@@ -26,9 +26,9 @@
<ol class="archive-list" id="archives"> <ol class="archive-list" id="archives">
</ol> </ol>
<div class="paste-actions"> <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> <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>
</div> </div>
+65 -66
View File
@@ -1,49 +1,48 @@
let pages = 0; let pages = 0;
let currentPage = 0; let currentPage = 0;
let limit = 25;
document.addEventListener('DOMContentLoaded', async () => { async function renderArchive(page, limit) {
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;
const archiveList = document.getElementById("archives"); const archiveList = document.getElementById("archives");
const pagesElement = document.getElementById("paste-pages"); const pagesElement = document.getElementById("paste-pages");
const previousButton = document.getElementById("previous-button"); const previousButton = document.getElementById("previous-button");
const nextButton = document.getElementById("next-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 { try {
archiveList.innerHTML = '<div class="loading-spinner"><div></div><div></div><div></div></div>';
const archiveData = await getPastes(page, limit); const archiveData = await getPastes(page, limit);
const pastes = archiveData.pastes; const pastes = archiveData.pastes;
pages = archiveData.pages; pages = archiveData.pages;
currentPage = page;
pagesElement.innerText = `${parseInt(page) + 1}/${pages}`; pagesElement.innerText = `${page + 1}/${pages}`;
previousButton.disabled = page <= 0; previousButton.disabled = page <= 0;
nextButton.disabled = page + 1 >= pages; nextButton.disabled = page + 1 >= pages;
if (pastes.length === 0) {
archiveList.innerHTML = '<div class="no-pastes">No pastes found</div>';
return;
}
let archiveListHTML = ""; let archiveListHTML = "";
if (pastes.length === 0) {
archiveListHTML = '<div class="no-pastes">No pastes found</div>';
} else {
pastes.forEach(paste => { pastes.forEach(paste => {
const title = paste.title.length > 30 ? paste.title.substring(0, 30) + '...' : paste.title; const title = paste.title.length > 30 ? paste.title.substring(0, 30) + '...' : paste.title;
archiveListHTML += ` archiveListHTML += `
@@ -56,12 +55,28 @@ document.addEventListener('DOMContentLoaded', async () => {
</li> </li>
`; `;
}); });
}
archiveList.innerHTML = archiveListHTML; archiveList.innerHTML = archiveListHTML;
} catch (error) { } catch (error) {
console.error('Error fetching pastes:', error); console.error('Error fetching pastes:', error);
archiveList.innerHTML = '<div class="error-message">Failed to load pastes. Please try again later.</div>'; 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) { async function getPastes(page, limit) {
@@ -95,43 +110,27 @@ function formatDate(unix) {
} }
function nextPage() { function nextPage() {
const queries = new URLSearchParams(window.location.search); if (currentPage + 1 >= pages) return;
let page = parseInt(queries.get("page")); currentPage++;
let limit = parseInt(queries.get("limit")); updateUrlAndRender();
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}`;
} }
function previousPage() { function previousPage() {
const queries = new URLSearchParams(window.location.search); if (currentPage <= 0) return;
let page = parseInt(queries.get("page")); currentPage--;
let limit = parseInt(queries.get("limit")); updateUrlAndRender();
if (isNaN(page) || page < 0) {
page = 0;
}
if (isNaN(limit) || limit < 1 || limit > 25) {
limit = 25;
}
if (page <= 0) {
return;
}
page--;
window.location.href = `/archive?page=${page}&limit=${limit}`;
} }
function updateUrlAndRender() {
const url = `/archive?page=${currentPage}&limit=${limit}`;
window.history.pushState({page: currentPage, limit: limit}, '', url);
renderArchive(currentPage, limit);
}
window.onpopstate = function(event) {
if (event.state) {
currentPage = event.state.page;
limit = event.state.limit;
renderArchive(currentPage, limit);
}
};
+3 -3
View File
@@ -139,7 +139,7 @@ function displayPastes() {
const pasteLink = document.createElement('a'); const pasteLink = document.createElement('a');
pasteLink.href = `/paste/${paste.id}`; pasteLink.href = `/${paste.id}`;
pasteLink.style.display = 'grid'; pasteLink.style.display = 'grid';
pasteLink.style.gridTemplateColumns = '2fr 1fr 1fr'; pasteLink.style.gridTemplateColumns = '2fr 1fr 1fr';
@@ -212,7 +212,7 @@ function logout() {
document.getElementById('confirm-logout').onclick = async function () { document.getElementById('confirm-logout').onclick = async function () {
try { try {
await fetch('/api/accounts/logout', { method: 'POST', credentials: 'include' }); await fetch('/api/accounts/logout', { method: 'POST'});
} catch { } } catch { }
window.location.href = '/login'; window.location.href = '/login';
}; };
@@ -264,7 +264,7 @@ function deleteAccount() {
confirmBtn.onclick = async function () { confirmBtn.onclick = async function () {
if (confirmBtn.disabled) return; if (confirmBtn.disabled) return;
try { try {
await fetch('/api/accounts/delete', { method: 'POST', credentials: 'include' }); await fetch('/api/accounts/delete', { method: 'POST'});
} catch { } } catch { }
window.location.href = '/register'; window.location.href = '/register';
}; };