From 2a8fff39c947b3b6622f0c5aa5f2365ec914ff35 Mon Sep 17 00:00:00 2001 From: AleksDw Date: Sat, 31 May 2025 14:37:06 +0200 Subject: [PATCH] Fix logout so it deletes token in database --- WebApp/ts/auth.ts | 24 +++++++++++++----------- WebApp/wwwroot/js/auth.js | 33 +++++++++++++++++++++++---------- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/WebApp/ts/auth.ts b/WebApp/ts/auth.ts index 9f91e0c..4e595ca 100644 --- a/WebApp/ts/auth.ts +++ b/WebApp/ts/auth.ts @@ -4,18 +4,17 @@ function deleteCookie(name: string): void { document.cookie = `${name}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`; } -function logoutUser(): void { - // Inform backend to remove cookie if necessary - fetch('/api/logout', { - method: 'POST', - credentials: 'include', - }).catch((err) => console.warn('Logout request failed:', err)); + async function logoutUser(): Promise { + await fetch("/api/auth/logout", { + method: "POST", + headers: { + "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(): void { @@ -46,7 +45,10 @@ function setupAuthUI(): void { if (logoutBtn) { logoutBtn.classList.toggle('d-none', !isAuthenticated); - logoutBtn.addEventListener('click', logoutUser); + logoutBtn.addEventListener('click', (e) => { + e.preventDefault(); + logoutUser(); + }); } } diff --git a/WebApp/wwwroot/js/auth.js b/WebApp/wwwroot/js/auth.js index d217220..b494245 100644 --- a/WebApp/wwwroot/js/auth.js +++ b/WebApp/wwwroot/js/auth.js @@ -1,18 +1,28 @@ "use strict"; // /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) { document.cookie = `${name}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`; } function logoutUser() { - // Inform backend to remove cookie if necessary - fetch('/api/logout', { - method: 'POST', - credentials: 'include', - }).catch((err) => console.warn('Logout request failed:', err)); - // Clear the auth cookie - deleteCookie('token'); - // Redirect to login page - window.location.href = 'index.html'; + return __awaiter(this, void 0, void 0, function* () { + yield fetch("/api/auth/logout", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + }); + deleteCookie('token'); + window.location.href = "/index.html"; + }); } function redirectToLogin() { window.location.href = 'login.html'; @@ -36,7 +46,10 @@ function setupAuthUI() { } if (logoutBtn) { logoutBtn.classList.toggle('d-none', !isAuthenticated); - logoutBtn.addEventListener('click', logoutUser); + logoutBtn.addEventListener('click', (e) => { + e.preventDefault(); + logoutUser(); + }); } } // Initialize on load