- Added a new profile page (my_profil.html) for users to manage their events and personal information. - Introduced a new CSS file (my_profil.css) for styling the profile page. - Created a JavaScript file (my_profil.js) to handle profile data retrieval, event registration management, and form submission. - Updated navigation logic (navigation.js) to dynamically display login/signup or event management links based on user authentication status. - Enhanced event creation and detail pages to support user-specific actions (registration/unregistration). - Improved login and signup processes to handle user data more robustly, including fallback user creation. - Refactored event overview to show user-specific events and registrations. - Added error handling and validation for user input in profile management.
117 lines
3.9 KiB
JavaScript
117 lines
3.9 KiB
JavaScript
const loginForm = document.getElementById('loginForm');
|
|
const emailInput = document.getElementById('email');
|
|
const passwortInput = document.getElementById('passwort');
|
|
const emailError = document.getElementById('emailError');
|
|
const passwortError = document.getElementById('passwortError');
|
|
|
|
const USERS_STORAGE_KEY = 'socialCookingUsers';
|
|
const CURRENT_USER_KEY = 'socialCookingCurrentUser';
|
|
|
|
// Liest alle registrierten Benutzer robust aus localStorage.
|
|
function getStoredUsers() {
|
|
try {
|
|
const raw = localStorage.getItem(USERS_STORAGE_KEY);
|
|
return raw ? JSON.parse(raw) : [];
|
|
} catch (error) {
|
|
console.error('Benutzerdaten konnten nicht gelesen werden.', error);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
// Speichert den aktiven Benutzer fuer nachfolgende Seiten.
|
|
function setCurrentUser(user) {
|
|
localStorage.setItem(CURRENT_USER_KEY, JSON.stringify(user));
|
|
}
|
|
|
|
// Erstellt einen Demo-Benutzer, falls fuer die E-Mail noch kein Account existiert.
|
|
function createFallbackUser(email, passwort) {
|
|
const localPart = email.split('@')[0] || 'Gast';
|
|
const normalized = localPart.replace(/[._-]/g, ' ').trim();
|
|
const guessedVorname = normalized ? normalized.split(' ')[0] : 'Gast';
|
|
|
|
return {
|
|
id: Date.now(),
|
|
vorname: guessedVorname.charAt(0).toUpperCase() + guessedVorname.slice(1),
|
|
nachname: '',
|
|
email,
|
|
passwort,
|
|
createdAt: new Date().toISOString(),
|
|
source: 'login-fallback'
|
|
};
|
|
}
|
|
|
|
// Validierungsfunktion
|
|
function validateForm(event) {
|
|
event.preventDefault();
|
|
|
|
let isValid = true;
|
|
|
|
// Email-Validierung
|
|
const emailValue = emailInput.value.trim();
|
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
const emailGroup = emailInput.parentElement;
|
|
|
|
if (!emailValue) {
|
|
emailGroup.classList.add('has-error');
|
|
emailError.textContent = 'Bitte gib deine E-Mail Adresse ein.';
|
|
isValid = false;
|
|
} else if (!emailRegex.test(emailValue)) {
|
|
emailGroup.classList.add('has-error');
|
|
emailError.textContent = 'Bitte gib eine gültige E-Mail Adresse ein.';
|
|
isValid = false;
|
|
} else {
|
|
emailGroup.classList.remove('has-error');
|
|
}
|
|
|
|
// Passwort-Validierung
|
|
const passwortValue = passwortInput.value;
|
|
const passwortGroup = passwortInput.parentElement;
|
|
|
|
if (!passwortValue) {
|
|
passwortGroup.classList.add('has-error');
|
|
passwortError.textContent = 'Bitte gib dein Passwort ein.';
|
|
isValid = false;
|
|
} else if (passwortValue.length < 6) {
|
|
passwortGroup.classList.add('has-error');
|
|
passwortError.textContent = 'Dein Passwort ist zu kurz. Bitte überprüfe dein Passwort.';
|
|
isValid = false;
|
|
} else {
|
|
passwortGroup.classList.remove('has-error');
|
|
}
|
|
|
|
// Wenn alle Validierungen bestanden, Benutzer pruefen und Session speichern.
|
|
if (isValid) {
|
|
const users = getStoredUsers();
|
|
const matchedUser = users.find(user => user.email?.toLowerCase() === emailValue.toLowerCase());
|
|
|
|
if (matchedUser && matchedUser.passwort !== passwortValue) {
|
|
passwortGroup.classList.add('has-error');
|
|
passwortError.textContent = 'Das Passwort ist nicht korrekt.';
|
|
return;
|
|
}
|
|
|
|
const userToLogin = matchedUser || createFallbackUser(emailValue, passwortValue);
|
|
setCurrentUser(userToLogin);
|
|
|
|
// Weiterleitung zur Event-Overview-Seite.
|
|
window.location.href = 'event_overview.html';
|
|
}
|
|
}
|
|
|
|
// Fehlerbehandlung bei Input-Änderung (entfernt Fehler wenn Benutzer korrigiert)
|
|
emailInput.addEventListener('input', function() {
|
|
const emailGroup = this.parentElement;
|
|
if (this.value.trim()) {
|
|
emailGroup.classList.remove('has-error');
|
|
}
|
|
});
|
|
|
|
passwortInput.addEventListener('input', function() {
|
|
const passwortGroup = this.parentElement;
|
|
if (this.value) {
|
|
passwortGroup.classList.remove('has-error');
|
|
}
|
|
});
|
|
|
|
// Form Submit Event
|
|
loginForm.addEventListener('submit', validateForm); |