feat(admin): add persistent login and improve audio playback error handling

This commit is contained in:
Hördle Bot
2025-11-21 20:52:08 +01:00
parent 95a3b09f52
commit 1d2a57352f

View File

@@ -39,12 +39,22 @@ export default function AdminPage() {
const [playingSongId, setPlayingSongId] = useState<number | null>(null); const [playingSongId, setPlayingSongId] = useState<number | null>(null);
const [audioElement, setAudioElement] = useState<HTMLAudioElement | null>(null); const [audioElement, setAudioElement] = useState<HTMLAudioElement | null>(null);
// Check for existing auth on mount
useEffect(() => {
const authToken = localStorage.getItem('hoerdle_admin_auth');
if (authToken === 'authenticated') {
setIsAuthenticated(true);
fetchSongs();
}
}, []);
const handleLogin = async () => { const handleLogin = async () => {
const res = await fetch('/api/admin/login', { const res = await fetch('/api/admin/login', {
method: 'POST', method: 'POST',
body: JSON.stringify({ password }), body: JSON.stringify({ password }),
}); });
if (res.ok) { if (res.ok) {
localStorage.setItem('hoerdle_admin_auth', 'authenticated');
setIsAuthenticated(true); setIsAuthenticated(true);
fetchSongs(); fetchSongs();
} else { } else {
@@ -147,9 +157,25 @@ export default function AdminPage() {
// Play new song // Play new song
const audio = new Audio(`/uploads/${song.filename}`); const audio = new Audio(`/uploads/${song.filename}`);
audio.play();
setAudioElement(audio); // Handle playback errors
setPlayingSongId(song.id); audio.onerror = () => {
alert(`Failed to load audio file: ${song.filename}\nThe file may be corrupted or missing.`);
setPlayingSongId(null);
setAudioElement(null);
};
audio.play()
.then(() => {
setAudioElement(audio);
setPlayingSongId(song.id);
})
.catch((error) => {
console.error('Playback error:', error);
alert(`Failed to play audio: ${error.message}`);
setPlayingSongId(null);
setAudioElement(null);
});
// Reset when song ends // Reset when song ends
audio.onended = () => { audio.onended = () => {