diff --git a/WebApp/ts/auth.ts b/WebApp/ts/auth.ts new file mode 100644 index 0000000..2363c03 --- /dev/null +++ b/WebApp/ts/auth.ts @@ -0,0 +1,58 @@ +// /js/auth.ts + +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)); + + // Clear the auth cookie + deleteCookie('token'); + + // Redirect to login page + window.location.href = 'index.html'; +} + +function redirectToLogin(): void { + window.location.href = 'login.html'; +} + +function checkAuth(): boolean { + // Basic auth check via presence of token cookie + return document.cookie.includes('token='); +} + +function setupAuthUI(): void { + const joinNowBtn = document.getElementById('joinnow-btn'); + const signInBtn = document.getElementById('signin-btn'); + const logoutBtn = document.getElementById('logout-btn'); + + const isAuthenticated = checkAuth(); + + if (joinNowBtn) { + joinNowBtn.classList.toggle('d-none', isAuthenticated); + joinNowBtn.addEventListener('click', redirectToLogin); + } + + if (signInBtn) { + signInBtn.classList.toggle('d-none', isAuthenticated); + signInBtn.addEventListener('click', redirectToLogin); + } + + if (logoutBtn) { + logoutBtn.classList.toggle('d-none', !isAuthenticated); + logoutBtn.addEventListener('click', logoutUser); + } + + // Hide all auth buttons initially until DOM loads + const hiddenBeforeLoad = document.querySelectorAll('.hidden-before-load'); + hiddenBeforeLoad.forEach(el => el.classList.remove('hidden-before-load')); +} + +// Initialize on load +document.addEventListener('DOMContentLoaded', setupAuthUI); diff --git a/WebApp/ts/login.ts b/WebApp/ts/login.ts new file mode 100644 index 0000000..def56be --- /dev/null +++ b/WebApp/ts/login.ts @@ -0,0 +1,38 @@ +document.addEventListener("DOMContentLoaded", () => { + const form = document.getElementById("loginForm") as HTMLFormElement; + const message = document.getElementById("message") as HTMLParagraphElement; + + form.addEventListener("submit", async (e) => { + e.preventDefault(); + message.textContent = ""; + + const email = (document.getElementById("email") as HTMLInputElement).value; + const password = (document.getElementById("password") as HTMLInputElement).value; + + try { + const response = await fetch("/api/auth/login", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ email, password }), + }); + + const data = await response.json(); + + if (!response.ok) { + message.textContent = data.message || "Login failed."; + return; + } + + document.cookie = `token=${data.token}; path=/; SameSite=Lax; Secure`; + message.style.color = "green"; + message.textContent = "Login successful!"; + + window.location.href = "/index.html"; + } catch (error) { + message.textContent = "Something went wrong."; + console.error(error); + } + }); +}); \ No newline at end of file diff --git a/WebApp/wwwroot/index.html b/WebApp/wwwroot/index.html index f7b67dc..45971c4 100644 --- a/WebApp/wwwroot/index.html +++ b/WebApp/wwwroot/index.html @@ -56,21 +56,21 @@
+ + -->

Events

@@ -92,5 +92,6 @@ + \ No newline at end of file diff --git a/WebApp/wwwroot/js/auth.js b/WebApp/wwwroot/js/auth.js new file mode 100644 index 0000000..e8be80f --- /dev/null +++ b/WebApp/wwwroot/js/auth.js @@ -0,0 +1,46 @@ +"use strict"; +// /js/auth.ts +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'; +} +function redirectToLogin() { + window.location.href = 'login.html'; +} +function checkAuth() { + // Basic auth check via presence of token cookie + return document.cookie.includes('token='); +} +function setupAuthUI() { + const joinNowBtn = document.getElementById('joinnow-btn'); + const signInBtn = document.getElementById('signin-btn'); + const logoutBtn = document.getElementById('logout-btn'); + const isAuthenticated = checkAuth(); + if (joinNowBtn) { + joinNowBtn.classList.toggle('d-none', isAuthenticated); + joinNowBtn.addEventListener('click', redirectToLogin); + } + if (signInBtn) { + signInBtn.classList.toggle('d-none', isAuthenticated); + signInBtn.addEventListener('click', redirectToLogin); + } + if (logoutBtn) { + logoutBtn.classList.toggle('d-none', !isAuthenticated); + logoutBtn.addEventListener('click', logoutUser); + } + // Hide all auth buttons initially until DOM loads + const hiddenBeforeLoad = document.querySelectorAll('.hidden-before-load'); + hiddenBeforeLoad.forEach(el => el.classList.remove('hidden-before-load')); +} +// Initialize on load +document.addEventListener('DOMContentLoaded', setupAuthUI); diff --git a/WebApp/wwwroot/js/eventView.js b/WebApp/wwwroot/js/eventView.js index 3b88e16..d5953cd 100644 --- a/WebApp/wwwroot/js/eventView.js +++ b/WebApp/wwwroot/js/eventView.js @@ -36,11 +36,11 @@ document.addEventListener("DOMContentLoaded", () => __awaiter(void 0, void 0, vo } catch (err) { if (container !== null) - container.innerHTML = `

To wydarzenie nie istnieje! Powrót ->

`; + container.innerHTML = `

To wydarzenie nie istnieje! Powr�t ->

`; } if (thisEvent == null) { if (container !== null) - container.innerHTML = `

Błąd we wczytywaniu wydarzenia. Powrót ->

`; + container.innerHTML = `

B��d we wczytywaniu wydarzenia. Powr�t ->

`; } else { const titleText = document.getElementById("titleText"); @@ -56,13 +56,13 @@ document.addEventListener("DOMContentLoaded", () => __awaiter(void 0, void 0, vo dateText.innerHTML = "When: " + newdateText + " " + newtimeText; //thisEvent.eventDate; organizerText.innerHTML = "Organized by: " + thisEvent.organisationName; if (org_id == thisEvent.organisationId) { - // Użytkownik jest organizacją, która - // stworzyła to wydarzenie + // U�ytkownik jest organizacj�, kt�ra + // stworzy�a to wydarzenie unhideElementById(document, "editBtn"); unhideElementById(document, "removeBtn"); } else if (org_id == -1) { - // Użytkownik jest wolontariuszem + // U�ytkownik jest wolontariuszem unhideElementById(document, "applyBtn"); } unhideElementById(document, "mainContainer"); @@ -78,7 +78,7 @@ document.addEventListener("DOMContentLoaded", () => __awaiter(void 0, void 0, vo if (!confirmed) return; try { - // Wysyła żądanie DELETE do API + // Wysy�a ��danie DELETE do API const response = yield fetch(`/api/events/${eventId}`, { method: "DELETE" }); diff --git a/WebApp/wwwroot/js/generalUseHelpers.js b/WebApp/wwwroot/js/generalUseHelpers.js index dc3e607..137fd53 100644 --- a/WebApp/wwwroot/js/generalUseHelpers.js +++ b/WebApp/wwwroot/js/generalUseHelpers.js @@ -29,7 +29,7 @@ export function getMyAccount() { return __awaiter(this, void 0, void 0, function* () { const res = yield fetch("/api/auth/my_account"); if (!res.ok) { - throw Error("Użytkownik niezalogowany!"); + throw Error("U�ytkownik niezalogowany!"); } const data = yield res.json(); return data; diff --git a/WebApp/wwwroot/js/login.js b/WebApp/wwwroot/js/login.js new file mode 100644 index 0000000..96cca61 --- /dev/null +++ b/WebApp/wwwroot/js/login.js @@ -0,0 +1,42 @@ +"use strict"; +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()); + }); +}; +document.addEventListener("DOMContentLoaded", () => { + const form = document.getElementById("loginForm"); + const message = document.getElementById("message"); + form.addEventListener("submit", (e) => __awaiter(void 0, void 0, void 0, function* () { + e.preventDefault(); + message.textContent = ""; + const email = document.getElementById("email").value; + const password = document.getElementById("password").value; + try { + const response = yield fetch("/api/auth/login", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ email, password }), + }); + const data = yield response.json(); + if (!response.ok) { + message.textContent = data.message || "Login failed."; + return; + } + document.cookie = `token=${data.token}; path=/; SameSite=Lax; Secure`; + message.style.color = "green"; + message.textContent = "Login successful!"; + window.location.href = "/index.html"; + } + catch (error) { + message.textContent = "Something went wrong."; + console.error(error); + } + })); +}); diff --git a/WebApp/wwwroot/login.html b/WebApp/wwwroot/login.html new file mode 100644 index 0000000..afb82fa --- /dev/null +++ b/WebApp/wwwroot/login.html @@ -0,0 +1,17 @@ + + + + + Login + + + +

Login

+
+

+

+ +

+
+ +