50 lines
1.8 KiB
JavaScript
50 lines
1.8 KiB
JavaScript
|
// Registrierungs-Event
|
||
|
document.getElementById('registerForm').addEventListener('submit', async (event) => {
|
||
|
event.preventDefault();
|
||
|
const username = document.getElementById('regUsername').value;
|
||
|
const email = document.getElementById('regEmail').value;
|
||
|
const password = document.getElementById('regPassword').value;
|
||
|
|
||
|
try {
|
||
|
const response = await fetch('http://localhost:8015/api/auth/register', {
|
||
|
method: 'POST',
|
||
|
headers: { 'Content-Type': 'application/json' },
|
||
|
body: JSON.stringify({ username, email, password })
|
||
|
});
|
||
|
|
||
|
const data = await response.json();
|
||
|
if (response.ok) {
|
||
|
alert('Registrierung erfolgreich! Bitte loggen Sie sich ein.');
|
||
|
} else {
|
||
|
alert(data.message || 'Registrierung fehlgeschlagen');
|
||
|
}
|
||
|
} catch (error) {
|
||
|
console.error('Fehler bei der Registrierung:', error);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// Login-Event
|
||
|
document.getElementById('loginForm').addEventListener('submit', async (event) => {
|
||
|
event.preventDefault();
|
||
|
const email = document.getElementById('loginEmail').value;
|
||
|
const password = document.getElementById('loginPassword').value;
|
||
|
|
||
|
try {
|
||
|
const response = await fetch('http://localhost:8015/api/auth/login', {
|
||
|
method: 'POST',
|
||
|
headers: { 'Content-Type': 'application/json' },
|
||
|
body: JSON.stringify({ email, password })
|
||
|
});
|
||
|
|
||
|
const data = await response.json();
|
||
|
if (response.ok) {
|
||
|
localStorage.setItem('token', data.token); // Token speichern
|
||
|
window.location.href = 'welcome.html'; // Weiterleitung zur Willkommensseite
|
||
|
} else {
|
||
|
alert(data.message || 'Login fehlgeschlagen');
|
||
|
}
|
||
|
} catch (error) {
|
||
|
console.error('Fehler beim Login:', error);
|
||
|
}
|
||
|
});
|