const particlesContainer = document.getElementById('particles');
for (let i = 0; i < 50; i++) {
const p = document.createElement('div');
p.className = 'particle';
p.style.left = Math.random() * 100 + '%';
p.style.animationDuration = (Math.random() * 20 + 10) + 's';
p.style.animationDelay = Math.random() * 20 + 's';
p.style.opacity = Math.random() * 0.5 + 0.1;
particlesContainer.appendChild(p);
}
fetch('/api/auth/check')
.then(r => r.json())
.then(data => {
if (data.loggedIn) window.location.href = '/admin';
});
const form = document.getElementById('loginForm');
const errorMsg = document.getElementById('errorMsg');
const errorText = document.getElementById('errorText');
const loginBtn = document.getElementById('loginBtn');
form.addEventListener('submit', async (e) => {
e.preventDefault();
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
loginBtn.disabled = true;
loginBtn.innerHTML = ' Wird geprüft...';
errorMsg.style.display = 'none';
try {
const res = await fetch('/api/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password })
});
const data = await res.json();
if (data.success) {
loginBtn.innerHTML = ' Erfolgreich!';
setTimeout(() => {
window.location.href = data.redirect;
}, 500);
} else {
throw new Error(data.message || 'Login fehlgeschlagen');
}
} catch (err) {
errorMsg.style.display = 'flex';
errorText.textContent = err.message;
loginBtn.disabled = false;
loginBtn.innerHTML = 'Anmelden';
}
});