Logo Perle trifft Primat

Perle trifft Primat

Registrieren

Login

Passwort zurücksetzen

Großes Bild
// Verifikationsmail erneut senden resendVerificationBtn.addEventListener('click', async () => { const email = resendEmailInput.value.trim(); if (!email) { showMessage(resendMessage, 'Bitte E-Mail eingeben', true); return; } try { const res = await fetch(`${API_BASE}/auth/resend-verification`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email }) }); const result = await res.json(); if (res.ok) { showMessage(resendMessage, result.message || 'Verifikationsmail erneut gesendet'); } else { showMessage(resendMessage, result.error || 'Fehler beim Senden', true); } } catch { showMessage(resendMessage, 'Serverfehler', true); } }); // Passwort zurücksetzen resetPasswordForm.addEventListener('submit', async e => { e.preventDefault(); const email = resetEmailInput.value.trim(); if (!email) { showMessage(resetPasswordMessage, 'Bitte E-Mail eingeben', true); return; } try { const res = await fetch(`${API_BASE}/auth/reset-password`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email }) }); const result = await res.json(); if (res.ok) { showMessage(resetPasswordMessage, result.message || 'Passwort-Reset Mail gesendet'); } else { showMessage(resetPasswordMessage, result.error || 'Fehler beim Senden', true); } } catch { showMessage(resetPasswordMessage, 'Serverfehler', true); } }); // Matching Profil anzeigen async function showMatchingProfile(username) { try { const res = await fetch(`${API_BASE}/profile/${username}`, { headers: { Authorization: 'Bearer ' + token } }); if (!res.ok) throw new Error('Profil konnte nicht geladen werden'); const profile = await res.json(); matchingProfileImage.src = UPLOAD_BASE + (profile.img || '').replace(/^\/uploads/, ''); matchingProfileImage.alt = 'Profil von ' + profile.username; currentViewingUsername = profile.username; matchFeedback.textContent = ''; } catch { alert('Matching Profil konnte nicht geladen werden.'); showSection(profilesSection); } } // Like/Dislike im Matching likeBtnMatch.addEventListener('click', async () => { if (!currentViewingUsername || currentViewingUsername === ownUsername) return; try { const res = await fetch(`${API_BASE}/match/${currentViewingUsername}/like`, { method: 'POST', headers: { Authorization: 'Bearer ' + token } }); const result = await res.json(); if (!res.ok) throw new Error(result.error || 'Fehler beim Like'); alert(result.message); await nextMatchingProfile(); } catch (error) { alert(error.message || 'Fehler beim Like'); } }); dislikeBtnMatch.addEventListener('click', async () => { if (!currentViewingUsername || currentViewingUsername === ownUsername) return; try { const res = await fetch(`${API_BASE}/match/${currentViewingUsername}/dislike`, { method: 'POST', headers: { Authorization: 'Bearer ' + token } }); const result = await res.json(); if (!res.ok) throw new Error(result.error || 'Fehler beim Dislike'); alert(result.message); await nextMatchingProfile(); } catch (error) { alert(error.message || 'Fehler beim Dislike'); } }); // Like/Dislike im Profil (fremd) document.getElementById('likeBtn').addEventListener('click', async () => { if (!currentViewingUsername || currentViewingUsername === ownUsername) return; try { const res = await fetch(`${API_BASE}/match/${currentViewingUsername}/like`, { method: 'POST', headers: { Authorization: 'Bearer ' + token } }); const result = await res.json(); if (!res.ok) throw new Error(result.error || 'Fehler beim Like'); alert(result.message); } catch (error) { alert(error.message || 'Fehler beim Like'); } }); document.getElementById('dislikeBtn').addEventListener('click', async () => { if (!currentViewingUsername || currentViewingUsername === ownUsername) return; try { const res = await fetch(`${API_BASE}/match/${currentViewingUsername}/dislike`, { method: 'POST', headers: { Authorization: 'Bearer ' + token } }); const result = await res.json(); if (!res.ok) throw new Error(result.error || 'Fehler beim Dislike'); alert(result.message); } catch (error) { alert(error.message || 'Fehler beim Dislike'); } }); // Button "Profil ansehen" im Matching viewProfileBtn.addEventListener('click', () => openProfile(currentViewingUsername)); // Nächstes Matching Profil async function nextMatchingProfile() { if (matchingProfiles.length === 0) { alert('Keine weiteren Matching-Profile.'); showSection(profilesSection); setActiveNavButton(navAllProfiles, navLoggedIn); return; } matchingProfiles.shift(); if (matchingProfiles.length > 0) { currentViewingUsername = matchingProfiles[0].username; await showMatchingProfile(currentViewingUsername); } else { alert('Keine weiteren Matching-Profile.'); showSection(profilesSection); setActiveNavButton(navAllProfiles, navLoggedIn); } } // Matches laden async function loadMatches() { try { const res = await fetch(`${API_BASE}/match`, { headers: { Authorization: 'Bearer ' + token } }); if (!res.ok) throw new Error('Fehler beim Laden der Matches'); const matches = await res.json(); myMatchesList.innerHTML = ''; if (matches.length === 0) { myMatchesList.innerHTML = '

Keine Matches gefunden.

'; return; } for (const username of matches) { const resProfile = await fetch(`${API_BASE}/profile/${username}`, { headers: { Authorization: 'Bearer ' + token } }); if (!resProfile.ok) continue; const profile = await resProfile.json(); const div = document.createElement('div'); div.className = 'match-profile'; div.innerHTML = ` Profilbild ${profile.name || profile.username} ` ; div.addEventListener('click', () => openProfile(username)); myMatchesList.appendChild(div); } } catch (error) { alert(error.message || 'Fehler beim Laden der Matches'); } } // Chat: Matches als Chatpartner async function loadChatMatches() { try { const res = await fetch(`${API_BASE}/match`, { headers: { Authorization: 'Bearer ' + token } }); if (!res.ok) throw new Error('Fehler beim Laden der Chat-Partner'); chatMatches = await res.json(); chatMatchesList.innerHTML = ''; if (chatMatches.length === 0) { chatMatchesList.innerHTML = '

Keine Matches zum Chatten.

'; return; } chatMatches.forEach(username => { const btn = document.createElement('div'); btn.className = 'chat-match'; btn.textContent = username; btn.addEventListener('click', () => openChat(username, btn)); chatMatchesList.appendChild(btn); }); } catch (error) { alert(error.message || 'Fehler beim Laden der Chat-Partner'); } } // Chat öffnen async function openChat(username, btn) { activeChatUser = username; Array.from(chatMatchesList.children).forEach(child => child.classList.remove('active')); if (btn) btn.classList.add('active'); chatMessagesDiv.innerHTML = '

Lade Nachrichten...

'; try { const res = await fetch(`${API_BASE}/chat/${username}`, { headers: { Authorization: 'Bearer ' + token } }); if (!res.ok) throw new Error('Fehler beim Laden der Nachrichten'); const messages = await res.json(); renderChatMessages(messages); } catch (error) { chatMessagesDiv.innerHTML = `

Fehler beim Laden der Nachrichten: ${error.message}

`; } } // Nachrichten anzeigen function renderChatMessages(messages) { chatMessagesDiv.innerHTML = ''; if (!messages || messages.length === 0) { chatMessagesDiv.innerHTML = '

Keine Nachrichten.

'; return; } messages.forEach(msg => { const div = document.createElement('div'); div.style.marginBottom = '10px'; div.style.textAlign = msg.sender === ownUsername ? 'right' : 'left'; div.innerHTML = `${msg.sender === ownUsername ? 'Du' : msg.sender}: ${escapeHtml(msg.message)}`; chatMessagesDiv.appendChild(div); }); chatMessagesDiv.scrollTop = chatMessagesDiv.scrollHeight; } // XSS Schutz function escapeHtml(text) { const map = { '&':'&', '<':'<', '>':'>', '"':'"', "'":''' }; return String(text ?? '').replace(/[&<>"']/g, m => map[m]); } // Chat senden chatSendBtn.addEventListener('click', async () => { const message = chatInput.value.trim(); if (!message) return; if (!activeChatUser) { alert('Bitte wähle zuerst einen Chat-Partner aus.'); return; } try { const res = await fetch(`${API_BASE}/chat/${activeChatUser}`, { method: 'POST', headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' }, body: JSON.stringify({ message }) }); const result = await res.json(); if (!res.ok) throw new Error(result.error || 'Fehler beim Senden der Nachricht'); chatInput.value = ''; await openChat(activeChatUser); // direkt neu laden } catch (error) { alert(error.message || 'Fehler beim Senden der Nachricht'); } }); // Enter sendet Nachricht chatInput.addEventListener('keydown', e => { if (e.key === 'Enter') { e.preventDefault(); chatSendBtn.click(); } }); // Zurück zur Übersicht von Profilen backToProfilesBtn.addEventListener('click', async () => { await loadAllProfiles(); setActiveNavButton(navAllProfiles, navLoggedIn); showSection(profilesSection); }); // Initialisierung async function initialize() { if (token) { await loadOwnProfileUsername(); logoutButton.style.display = 'inline-block'; navLoggedOut.style.display = 'none'; navLoggedIn.style.display = 'flex'; setActiveNavButton(navAllProfiles, navLoggedIn); await loadAllProfiles(); await openProfile(ownUsername); } else { logoutButton.style.display = 'none'; navLoggedIn.style.display = 'none'; navLoggedOut.style.display = 'flex'; setActiveNavButton(navLoginRegister, navLoggedOut); showSection(authSection); } } initialize();