Fix logout so it deletes token in database

This commit is contained in:
AleksDw
2025-05-31 14:37:06 +02:00
parent b194819b6e
commit 2a8fff39c9
2 changed files with 36 additions and 21 deletions

View File

@@ -4,18 +4,17 @@ function deleteCookie(name: string): void {
document.cookie = `${name}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`; document.cookie = `${name}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`;
} }
function logoutUser(): void { async function logoutUser(): Promise<void> {
// Inform backend to remove cookie if necessary await fetch("/api/auth/logout", {
fetch('/api/logout', { method: "POST",
method: 'POST', headers: {
credentials: 'include', "Content-Type": "application/json",
}).catch((err) => console.warn('Logout request failed:', err)); },
});
// Clear the auth cookie
deleteCookie('token'); deleteCookie('token');
// Redirect to login page window.location.href = "/index.html";
window.location.href = 'index.html';
} }
function redirectToLogin(): void { function redirectToLogin(): void {
@@ -46,7 +45,10 @@ function setupAuthUI(): void {
if (logoutBtn) { if (logoutBtn) {
logoutBtn.classList.toggle('d-none', !isAuthenticated); logoutBtn.classList.toggle('d-none', !isAuthenticated);
logoutBtn.addEventListener('click', logoutUser); logoutBtn.addEventListener('click', (e) => {
e.preventDefault();
logoutUser();
});
} }
} }

View File

@@ -1,18 +1,28 @@
"use strict"; "use strict";
// /js/auth.ts // /js/auth.ts
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
function deleteCookie(name) { function deleteCookie(name) {
document.cookie = `${name}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`; document.cookie = `${name}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`;
} }
function logoutUser() { function logoutUser() {
// Inform backend to remove cookie if necessary return __awaiter(this, void 0, void 0, function* () {
fetch('/api/logout', { yield fetch("/api/auth/logout", {
method: 'POST', method: "POST",
credentials: 'include', headers: {
}).catch((err) => console.warn('Logout request failed:', err)); "Content-Type": "application/json",
// Clear the auth cookie },
});
deleteCookie('token'); deleteCookie('token');
// Redirect to login page window.location.href = "/index.html";
window.location.href = 'index.html'; });
} }
function redirectToLogin() { function redirectToLogin() {
window.location.href = 'login.html'; window.location.href = 'login.html';
@@ -36,7 +46,10 @@ function setupAuthUI() {
} }
if (logoutBtn) { if (logoutBtn) {
logoutBtn.classList.toggle('d-none', !isAuthenticated); logoutBtn.classList.toggle('d-none', !isAuthenticated);
logoutBtn.addEventListener('click', logoutUser); logoutBtn.addEventListener('click', (e) => {
e.preventDefault();
logoutUser();
});
} }
} }
// Initialize on load // Initialize on load