This commit is contained in:
jeremygan2021
2026-03-20 16:18:58 +08:00
parent 3bdb055b83
commit 7caff4d72d
6 changed files with 312 additions and 31 deletions

View File

@@ -297,28 +297,62 @@
</div>
<script>
// Use secret from backend
const ACCESS_SECRET = "{{ secret }}";
document.addEventListener('DOMContentLoaded', checkAuthStatus);
// Password Check Logic
function checkSecret() {
const input = document.getElementById('secret-input');
const error = document.getElementById('secret-error');
const overlay = document.getElementById('password-overlay');
if (input.value === ACCESS_SECRET) {
overlay.style.opacity = '0';
setTimeout(() => {
overlay.style.display = 'none';
}, 500);
} else {
error.classList.remove('hidden');
input.value = '';
input.focus();
async function checkAuthStatus() {
try {
const res = await fetch('/api/admin/check-auth');
const data = await res.json();
if (data.authenticated) {
hideOverlay();
} else {
showOverlay();
}
} catch (e) {
showOverlay();
}
}
function showOverlay() {
document.getElementById('password-overlay').style.display = 'flex';
}
function hideOverlay() {
const overlay = document.getElementById('password-overlay');
overlay.style.opacity = '0';
setTimeout(() => {
overlay.style.display = 'none';
}, 500);
}
async function checkSecret() {
const input = document.getElementById('secret-input');
const error = document.getElementById('secret-error');
const password = input.value;
try {
const res = await fetch('/api/admin/login', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({password: password})
});
const data = await res.json();
if (data.success) {
error.classList.add('hidden');
hideOverlay();
} else {
error.classList.remove('hidden');
input.value = '';
input.focus();
}
} catch (e) {
error.textContent = '网络错误';
error.classList.remove('hidden');
}
}
// Allow pressing Enter to submit password
document.getElementById('secret-input').addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
checkSecret();