"feat: Implement auto logout functionality via static JS

This commit is contained in:
2025-05-29 19:29:46 +02:00
parent f4fbf92055
commit d26f8b0d93
4 changed files with 268 additions and 23 deletions

View File

@@ -584,6 +584,7 @@
<script src="{{ url_for('static', filename='js/csp-violation-handler.js') }}"></script>
{% if current_user.is_authenticated %}
<script src="{{ url_for('static', filename='js/notifications.js') }}"></script>
<script src="{{ url_for('static', filename='js/auto-logout.js') }}"></script>
{% endif %}
<!-- Additional JavaScript Functions -->

View File

@@ -621,33 +621,32 @@
function setupAutoLogout() {
const autoLogoutSelect = document.getElementById('auto-logout');
function resetLogoutTimer() {
if (logoutTimer) {
clearTimeout(logoutTimer);
}
// Event-Listener für Änderungen der Auto-Logout-Einstellung
autoLogoutSelect.addEventListener('change', async function() {
const newTimeout = this.value;
const minutes = parseInt(autoLogoutSelect.value);
if (minutes && minutes !== 'never') {
logoutTimer = setTimeout(() => {
if (confirm('Sie werden aufgrund von Inaktivität abgemeldet. Möchten Sie angemeldet bleiben?')) {
resetLogoutTimer();
} else {
window.location.href = '/logout';
try {
const response = await fetch('/api/user/setting', {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || ''
},
body: JSON.stringify({ auto_logout: newTimeout })
});
if (response.ok) {
// Globales Auto-Logout-System benachrichtigen
if (window.autoLogoutManager) {
window.autoLogoutManager.updateSettings(newTimeout);
}
}, minutes * 60 * 1000);
showFlashMessage('Auto-Logout-Einstellung aktualisiert', 'success');
}
} catch (error) {
console.error('Fehler beim Aktualisieren der Auto-Logout-Einstellung:', error);
showFlashMessage('Fehler beim Speichern der Einstellung', 'error');
}
}
// Reset timer on any user activity
['mousedown', 'mousemove', 'keypress', 'scroll', 'touchstart', 'click'].forEach(event => {
document.addEventListener(event, resetLogoutTimer, { passive: true });
});
// Initial setup
resetLogoutTimer();
// Update timer when setting changes
autoLogoutSelect.addEventListener('change', resetLogoutTimer);
}
// Enhanced toggle switches with keyboard support