Moved authentification code into js/auth.js

This commit is contained in:
Josh-Dev-Quest committed 2026-03-28 13:43:05 +01:00
1 parent fec4f513c8
commit e3724a4842
8 files changed
+330 -364

No files matched your search

+3 -58
View File
@@ -876,65 +876,10 @@
</main>
</div>
<div id="toast" class="toast"><p id="toast-message"></p></div>
<script src="js/auth.js"></script>
<script>
// ==================== AUTH & REDIRECT ====================
const API_BASE = "http://127.0.0.1:5000/api";
let currentUser = null;
async function checkAuth() {
try {
const res = await fetch(`${API_BASE}/me`, {
credentials: "include",
});
if (res.ok) {
currentUser = await res.json();
return true;
} else {
window.location.href = "login.html";
return false;
}
} catch (err) {
window.location.href = "login.html";
return false;
}
}
function updateUserMenu() {
const container = document.getElementById("user-menu");
if (currentUser) {
container.innerHTML = `
<span class="username"><i class="fas fa-user"></i> ${escapeHtml(currentUser.username)}</span>
<button id="logout-btn" class="logout-btn"><i class="fas fa-sign-out-alt"></i> Logout</button>
`;
document
.getElementById("logout-btn")
?.addEventListener("click", logout);
}
}
async function logout() {
await fetch(`${API_BASE}/logout`, {
method: "POST",
credentials: "include",
});
window.location.href = "login.html";
}
function escapeHtml(str) {
if (!str) return "";
return str.replace(/[&<>]/g, function (m) {
if (m === "&") return "&amp;";
if (m === "<") return "&lt;";
if (m === ">") return "&gt;";
return m;
});
}
// ==================== MAP CODE (from your file, but with credentials added) ====================
// ==================== MAP CODE ====================
(function () {
// ==================== CONFIG ====================
// API_BASE is already defined outside
// ==================== STATE =====================
let map;
let currentJourney = {
@@ -1462,7 +1407,7 @@
// ==================== AUTHENTICATION CHECK ====================
document.addEventListener("DOMContentLoaded", async () => {
const authenticated = await checkAuth();
const authenticated = await checkAuthAndRedirect();
if (authenticated) {
updateUserMenu();
window.startMap(); // call the map initializer from the IIFE