diff --git a/auth/auth.js b/auth/auth.js index 266e09d..caa68c3 100755 --- a/auth/auth.js +++ b/auth/auth.js @@ -40,6 +40,23 @@ app.post("/api/login", async (req,res)=>{ res.status(200).send({ ok:true }); }); +// Logout endpoint +app.post("/api/logout", (req, res) => { + const sid = req.cookies.SESSIONID; + if (sid && SESSIONS[sid]) { + delete SESSIONS[sid]; + } + // Cookie löschen + res.clearCookie("SESSIONID", { + httpOnly: true, + secure: true, + domain: ".server.schooltech.ch", + sameSite: "None", + path: "/" + }); + return res.status(200).send({ ok: true }); +}); + // Event logging endpoint for frontend button presses app.post('/api/event', (req,res)=>{ const svc = req.body.service || req.body.action || 'unknown'; @@ -58,4 +75,13 @@ app.get("/internal/auth", (req,res)=>{ return res.sendStatus(401); }); +// Status endpoint (unter /api so dass Nginx /api/ auf appserverauth proxyt) +app.get("/api/status", (req, res) => { + const sid = req.cookies.SESSIONID; + if (sid && SESSIONS[sid]) { + return res.status(200).send({ ok: true, user: SESSIONS[sid].user }); + } + return res.status(401).send({ ok: false }); +}); + app.listen(3000, ()=>console.log("Auth-Service läuft auf 3000")); diff --git a/public/app.js b/public/app.js index cd605c4..b19b852 100755 --- a/public/app.js +++ b/public/app.js @@ -100,6 +100,36 @@ function logout() { loginModal.style.display = "block"; } +// Setzt Login-Button so, dass er Logout macht +function switchToLogout() { + loginBtn.textContent = "Logout"; + loginBtn.onclick = async () => { + // Option: serverseitiges Logout anstoßen (löscht Cookie / Session) + try { + await fetch("/api/logout", { method: "POST" }); + } catch (e) { + console.warn("Logout request failed:", e); + } + performLocalLogout(); + }; +} + +// Setzt Login-Button zurück auf Login (zeigt Modal) +function switchToLogin() { + loginBtn.textContent = "Login"; + loginBtn.onclick = () => { loginModal.style.display = "block"; }; +} + +// Lokale UI-Aufräumarbeiten bei Logout +function performLocalLogout() { + loggedIn = false; + iframe.src = ""; + iframe.style.display = "none"; + nav.innerHTML = ""; + loginModal.style.display = "block"; + switchToLogin(); +} + // Setzen des Cookies für die Sitzung function setSessionCookie(res, user) { res.cookie("SESSIONID", "session-"+user, { @@ -109,4 +139,4 @@ function setSessionCookie(res, user) { sameSite: "None", path: "/" }); -} +} \ No newline at end of file