forked from sem720/Join
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
253 lines (214 loc) · 7.08 KB
/
script.js
File metadata and controls
253 lines (214 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
let logStatus;
const dbUrl =
"https://join-c8725-default-rtdb.europe-west1.firebasedatabase.app";
/**
* Handles user login process with email and password
* @returns {void}
*/
function login() {
if (!validateForm()) {
return;
}
let email = document.getElementById("Email").value.trim().toLowerCase();
let password = document.getElementById("Password").value;
const firebaseKey = email.replace(/\./g, "_").replace(/@/g, "-");
fetch(`${dbUrl}/users/${firebaseKey}.json`)
.then((response) => response.json())
.then((user) => {
if (user && user.password === password) {
saveUserToLocalStorage(user, email);
setTimeout(() => {
window.location.href = "../summary/summary.html";
}, 500);
} else {
document.getElementById("render-alert").innerHTML =
"The email or password is incorrect.";
}
})
.catch((error) => console.error("Fehler:", error));
document.getElementById("Password").value = "";
document.getElementById("Email").value = "";
}
function checkForm() {
let email = document.getElementById("Email").value.trim();
let password = document.getElementById("Password").value.trim();
let loginButton = document.getElementById("login-button");
const isFormValid = email !== "" && password !== "";
loginButton.disabled = !isFormValid;
}
document.getElementById("Email").addEventListener("input", checkForm);
document.getElementById("Password").addEventListener("input", checkForm);
/**
* Saves user data to localStorage
* @param {Object} user - User object from database
* @param {string} email - User's email address
* @returns {void}
*/
function saveUserToLocalStorage(user, email) {
const userData = {
name: user.name,
email: email,
};
localStorage.setItem("user", JSON.stringify(userData));
}
/**
* Handles guest login without credentials
* @returns {void}
*/
function guestLogin() {
const guestData = {
name: "Guest",
email: "guest@example.com",
};
localStorage.setItem("user", JSON.stringify(guestData));
setTimeout(() => {
window.location.href = "../summary/summary.html";
}, 500);
}
/**
* Checks if user is logged in and redirects if not
* @returns {void}
*/
function checkUserStatus() {
const user = JSON.parse(localStorage.getItem("user"));
if (!user) {
alert("Bitte loggen Sie sich ein.");
window.location.href = "../index.html";
} else {
console.log(`Willkommen zurück, ${user.name}!`);
}
}
/**
* Validates login form inputs
* @returns {boolean} True if form is valid
*/
function validateForm() {
const email = document.getElementById("Email").value.trim().toLowerCase();
const password = document.getElementById("Password").value;
let error = "";
switch (true) {
case !email:
error = "Please enter your email address";
break;
case !validateEmail(email):
error = "Please enter a valid email address";
break;
case !password:
error = "Please enter a password";
break;
}
if (error) {
document.getElementById("render-alert").innerHTML = error;
return false;
}
return true;
}
/**
* Validates email format
* @param {string} email - Email to validate
* @returns {boolean} True if valid email format
*/
function validateEmail(email) {
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailPattern.test(email);
}
/**
* Überprüft, ob das angegebene Element vorhanden ist.
* @param {string} elementId - ID des HTML-Elements.
* @returns {HTMLElement|null} Das gefundene Element oder null, wenn es nicht existiert.
*/
function getElementByIdSafe(elementId) {
const element = document.getElementById(elementId);
if (!element) {
console.warn(`Element mit ID '${elementId}' nicht gefunden.`);
}
return element;
}
/**
* Versteckt oder zeigt Icons basierend auf dem Passwortfeldinhalt.
* @param {HTMLElement} passwordInput - Das Passwort-Eingabefeld.
* @param {HTMLElement} lockIcon - Das Schlosssymbol.
* @param {HTMLElement} eyeSlashIcon - Das durchgestrichene Augensymbol.
* @param {HTMLElement} eyeIcon - Das Augensymbol.
* @returns {void}
*/
function togglePasswordIcons(passwordInput, lockIcon, eyeSlashIcon, eyeIcon) {
if (passwordInput.value) {
lockIcon?.classList.add("d_none");
eyeSlashIcon?.classList.remove("d_none");
} else {
lockIcon?.classList.remove("d_none");
eyeSlashIcon?.classList.add("d_none");
eyeIcon?.classList.add("d_none");
passwordInput.type = "password";
}
}
/**
* Zeigt das Passwort als Klartext, wenn das Augensymbol (eyeSlashIcon) geklickt wird.
* @param {HTMLElement} passwordInput - Das Passwort-Eingabefeld.
* @param {HTMLElement} eyeSlashIcon - Das durchgestrichene Augensymbol.
* @param {HTMLElement} eyeIcon - Das Augensymbol.
* @returns {void}
*/
function showPasswordText(passwordInput, eyeSlashIcon, eyeIcon) {
passwordInput.type = "text";
eyeSlashIcon.classList.add("d_none");
eyeIcon?.classList.remove("d_none");
}
/**
* Versteckt das Passwort und zeigt das durchgestrichene Augensymbol, wenn das Augensymbol (eyeIcon) geklickt wird.
* @param {HTMLElement} passwordInput - Das Passwort-Eingabefeld.
* @param {HTMLElement} eyeIcon - Das Augensymbol.
* @param {HTMLElement} eyeSlashIcon - Das durchgestrichene Augensymbol.
* @returns {void}
*/
function hidePasswordText(passwordInput, eyeIcon, eyeSlashIcon) {
passwordInput.type = "password";
eyeIcon.classList.add("d_none");
eyeSlashIcon?.classList.remove("d_none");
}
/**
* Initialisiert das Umschalten der Passwortsichtbarkeit für das angegebene Eingabefeld.
* @param {string} inputId - Die ID des Passwort-Eingabefeldes.
* @returns {void}
*/
function setupPasswordToggle(inputId) {
const passwordInput = getElementByIdSafe(inputId);
if (!passwordInput) return;
const container = passwordInput.parentElement;
if (!container) return;
const lockIcon = container.querySelector(".lock-icon");
const eyeSlashIcon = container.querySelector(".eye-icon");
const eyeIcon = container.querySelector(".eye-slash-icon");
passwordInput.addEventListener("input", () => {
togglePasswordIcons(passwordInput, lockIcon, eyeSlashIcon, eyeIcon);
});
eyeSlashIcon?.addEventListener("click", () => {
showPasswordText(passwordInput, eyeSlashIcon, eyeIcon);
});
eyeIcon?.addEventListener("click", () => {
hidePasswordText(passwordInput, eyeIcon, eyeSlashIcon);
});
}
document.querySelectorAll("#Password, #PasswordCon").forEach((el) => {
setupPasswordToggle(el.id);
});
/* Email input validation */
const emailInput = document.getElementById('Email');
const isValidEmail = (email) => {
// einfache Regex für grundlegende Validierung
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return regex.test(email);
};
emailInput.addEventListener('input', () => {
const value = emailInput.value;
if (value === "") {
emailInput.classList.remove('valid', 'invalid');
} else if (isValidEmail(value)) {
emailInput.classList.add('valid');
emailInput.classList.remove('invalid');
} else {
emailInput.classList.add('invalid');
emailInput.classList.remove('valid');
}
});