Files
test/public/dashboard.js
Ali 1d539e44ac
Some checks failed
Build & Push Image / build-image (push) Has been cancelled
auth
2026-02-24 21:49:23 +09:00

58 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// dashboard.js
import { authFetch } from "api.js";
// ElementReferenzen
const welcomeMessage = document.getElementById("welcomeMessage");
const todosList = document.getElementById("todosList");
const errorMessage = document.getElementById("errorMessage");
const logoutBtn = document.getElementById("logoutBtn");
// Prüfe ob ein JWT vorhanden ist
const token = localStorage.getItem("token");
if (!token) {
// Kein Token → zurück zur LoginSeite
window.location.href = "login.html";
}
// Logout Token löschen + weiterleiten
logoutBtn.addEventListener("click", () => {
localStorage.removeItem("token");
window.location.href = "login.html";
});
// Lade Todos vom Backend
async function loadTodos() {
try {
const res = await authFetch("/todos"); // fetchWrapper sendet den JWT Automatisch
if (!res.ok) {
errorMessage.textContent = "Fehler beim Abrufen der Todos!";
if (res.status === 401) {
window.location.href = "login.html"; // bei ungültigem Token zum Login
}
return;
}
const todos = await res.json();
// Zeige Todos in der Liste an
todosList.innerHTML = "";
if (Array.isArray(todos) && todos.length > 0) {
todos.forEach(todo => {
const li = document.createElement("li");
li.textContent = todo.task;
todosList.appendChild(li);
});
} else {
todosList.innerHTML = "<li>Keine Todos gefunden.</li>";
}
} catch (err) {
console.error("Fetch Error:", err);
errorMessage.textContent = "Serverfehler beim Laden!";
}
}
// Willkommenstext setzen
welcomeMessage.textContent = "Willkommen zum Dashboard!";
// Rufe die Todos ab
loadTodos();