auth frontend

This commit is contained in:
AleksDw
2025-05-31 13:34:18 +02:00
parent 740f8a955d
commit 42e468f28f
8 changed files with 213 additions and 11 deletions

46
WebApp/wwwroot/js/auth.js Normal file
View File

@@ -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);

View File

@@ -36,11 +36,11 @@ document.addEventListener("DOMContentLoaded", () => __awaiter(void 0, void 0, vo
}
catch (err) {
if (container !== null)
container.innerHTML = `<p class="text-danger">To wydarzenie nie istnieje! <a href="/" style="color:#2898BD;">Powrót -></a></p>`;
container.innerHTML = `<p class="text-danger">To wydarzenie nie istnieje! <a href="/" style="color:#2898BD;">Powr<EFBFBD>t -></a></p>`;
}
if (thisEvent == null) {
if (container !== null)
container.innerHTML = `<p class="text-danger">Błąd we wczytywaniu wydarzenia. <a href="/" style="color:#2898BD;">Powrót -></a></p>`;
container.innerHTML = `<p class="text-danger">B<EFBFBD><EFBFBD>d we wczytywaniu wydarzenia. <a href="/" style="color:#2898BD;">Powr<EFBFBD>t -></a></p>`;
}
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<EFBFBD>ytkownik jest organizacj<EFBFBD>, kt<EFBFBD>ra
// stworzy<EFBFBD>a to wydarzenie
unhideElementById(document, "editBtn");
unhideElementById(document, "removeBtn");
}
else if (org_id == -1) {
// Użytkownik jest wolontariuszem
// U<EFBFBD>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<EFBFBD>a <EFBFBD><EFBFBD>danie DELETE do API
const response = yield fetch(`/api/events/${eventId}`, {
method: "DELETE"
});

View File

@@ -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<EFBFBD>ytkownik niezalogowany!");
}
const data = yield res.json();
return data;

View File

@@ -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);
}
}));
});