-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.html
More file actions
358 lines (323 loc) · 12.9 KB
/
Copy pathjavascript.html
File metadata and controls
358 lines (323 loc) · 12.9 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<script>
const signUpButton = document.getElementById('signUp');
const signInButton = document.getElementById('signIn');
const container = document.getElementById('container');
const signupForm = document.getElementById('signupForm');
const loginForm = document.getElementById('loginForm');
const signupMessage = document.getElementById('signupMessage');
const loginMessage = document.getElementById('loginMessage');
const resendVerificationLink = document.getElementById('resendVerificationLink');
// Modals and related elements
const forgotPasswordLink = document.getElementById('forgotPasswordLink');
const emailRequestModal = document.getElementById('emailRequestModal');
const otpEntryModal = document.getElementById('otpEntryModal');
const changePasswordModal = document.getElementById('changePasswordModal');
const forgotEmailInput = document.getElementById('forgotEmail');
const sendOtpButton = document.getElementById('sendOtpButton');
const forgotEmailMessage = document.getElementById('forgotEmailMessage');
const otpInput = document.getElementById('otpInput');
const verifyOtpButton = document.getElementById('verifyOtpButton');
const resendOtpButton = document.getElementById('resendOtpButton');
const otpTimerDisplay = document.getElementById('otpTimer');
const otpMessage = document.getElementById('otpMessage');
const newPasswordInput = document.getElementById('newPassword');
const confirmNewPasswordInput = document.getElementById('confirmNewPassword');
const resetPasswordButton = document.getElementById('resetPasswordButton');
const changePasswordMessage = document.getElementById('changePasswordMessage');
const closeButtons = document.querySelectorAll('.close-button');
let forgotPasswordEmail = ''; // To store the email for OTP/password reset
let otpCountdownInterval;
// --- UI Animation Handlers ---
signUpButton.addEventListener('click', () => {
container.classList.add("right-panel-active");
clearMessages();
clearFormFields();
});
signInButton.addEventListener('click', () => {
container.classList.remove("right-panel-active");
clearMessages();
clearFormFields();
});
// --- Form Submission Handlers ---
signupForm.addEventListener('submit', (e) => {
e.preventDefault();
const name = document.getElementById('signupName').value;
const email = document.getElementById('signupEmail').value;
const password = document.getElementById('signupPassword').value;
if (!name || !email || !password) {
showMessage(signupMessage, 'error', 'Please fill all fields.');
return;
}
if (!isValidEmail(email)) {
showMessage(signupMessage, 'error', 'Please enter a valid email address.');
return;
}
showMessage(signupMessage, 'neutral', 'Registering...');
google.script.run
.withSuccessHandler((response) => {
if (response.success) {
showMessage(signupMessage, 'success', response.message);
clearFormFields();
// container.classList.remove("right-panel-active"); // Switch to sign-in form
} else {
showMessage(signupMessage, 'error', response.message);
}
})
.withFailureHandler((error) => {
showMessage(signupMessage, 'error', 'An error occurred during registration.');
console.error(error);
})
.processSignup({ name, email, password });
});
loginForm.addEventListener('submit', (e) => {
e.preventDefault();
const email = document.getElementById('loginEmail').value;
const password = document.getElementById('loginPassword').value;
if (!email || !password) {
showMessage(loginMessage, 'error', 'Please enter email and password.');
return;
}
if (!isValidEmail(email)) {
showMessage(loginMessage, 'error', 'Please enter a valid email address.');
return;
}
showMessage(loginMessage, 'neutral', 'Signing in...');
resendVerificationLink.style.display = 'none'; // Hide resend link initially
google.script.run
.withSuccessHandler((response) => {
if (response.success) {
showMessage(loginMessage, 'success', response.message);
clearFormFields();
setTimeout(() => { window.location.href = response.redirectUrl; }, 1000); // Redirect after short delay
} else {
showMessage(loginMessage, 'error', response.message);
if (response.unverified) {
resendVerificationLink.style.display = 'block'; // Show resend link if unverified
}
}
})
.withFailureHandler((error) => {
showMessage(loginMessage, 'error', 'An error occurred during login.');
console.error(error);
})
.processLogin({ email, password });
});
// --- Forgot Password Flow Handlers ---
forgotPasswordLink.addEventListener('click', (e) => {
e.preventDefault();
showModal(emailRequestModal);
forgotEmailInput.value = '';
showMessage(forgotEmailMessage, 'neutral', '');
});
sendOtpButton.addEventListener('click', () => {
forgotPasswordEmail = forgotEmailInput.value.trim();
if (!isValidEmail(forgotPasswordEmail)) {
showMessage(forgotEmailMessage, 'error', 'Please enter a valid email address.');
return;
}
showMessage(forgotEmailMessage, 'neutral', 'Sending OTP...');
google.script.run
.withSuccessHandler((response) => {
if (response.success) {
showMessage(forgotEmailMessage, 'success', response.message);
setTimeout(() => {
hideModal(emailRequestModal);
showModal(otpEntryModal);
startOtpTimer(10); // 10 minutes
otpInput.value = ''; // Clear OTP input
showMessage(otpMessage, 'neutral', ''); // Clear OTP message
}, 1000);
} else {
showMessage(forgotEmailMessage, 'error', response.message);
}
})
.withFailureHandler((error) => {
showMessage(forgotEmailMessage, 'error', 'Error sending OTP.');
console.error(error);
})
.sendPasswordResetOTP(forgotPasswordEmail);
});
verifyOtpButton.addEventListener('click', () => {
const otp = otpInput.value.trim();
if (otp.length !== 6 || !/^\d+$/.test(otp)) {
showMessage(otpMessage, 'error', 'Please enter a valid 6-digit OTP.');
return;
}
showMessage(otpMessage, 'neutral', 'Verifying OTP...');
google.script.run
.withSuccessHandler((response) => {
if (response.success) {
showMessage(otpMessage, 'success', response.message);
clearInterval(otpCountdownInterval); // Stop timer
setTimeout(() => {
hideModal(otpEntryModal);
showModal(changePasswordModal);
newPasswordInput.value = '';
confirmNewPasswordInput.value = '';
showMessage(changePasswordMessage, 'neutral', '');
}, 1000);
} else {
showMessage(otpMessage, 'error', response.message);
}
})
.withFailureHandler((error) => {
showMessage(otpMessage, 'error', 'Error verifying OTP.');
console.error(error);
})
.verifyOTP(forgotPasswordEmail, otp);
});
resendOtpButton.addEventListener('click', () => {
if (!forgotPasswordEmail) {
showMessage(otpMessage, 'error', 'No email registered for password reset. Please go back and enter your email.');
return;
}
showMessage(otpMessage, 'neutral', 'Resending OTP...');
google.script.run
.withSuccessHandler((response) => {
if (response.success) {
showMessage(otpMessage, 'success', response.message);
startOtpTimer(10); // Restart timer
otpInput.value = '';
} else {
showMessage(otpMessage, 'error', response.message);
}
})
.withFailureHandler((error) => {
showMessage(otpMessage, 'error', 'Error resending OTP.');
console.error(error);
})
.sendPasswordResetOTP(forgotPasswordEmail);
});
resetPasswordButton.addEventListener('click', () => {
const newPassword = newPasswordInput.value;
const confirmNewPassword = confirmNewPasswordInput.value;
if (!newPassword || !confirmNewPassword) {
showMessage(changePasswordMessage, 'error', 'Please enter and confirm your new password.');
return;
}
if (newPassword !== confirmNewPassword) {
showMessage(changePasswordMessage, 'error', 'Passwords do not match.');
return;
}
if (newPassword.length < 6) { // Basic password length validation
showMessage(changePasswordMessage, 'error', 'Password must be at least 6 characters long.');
return;
}
showMessage(changePasswordMessage, 'neutral', 'Resetting password...');
google.script.run
.withSuccessHandler((response) => {
if (response.success) {
showMessage(changePasswordMessage, 'success', response.message);
setTimeout(() => {
hideModal(changePasswordModal);
// Optionally, switch back to login form or clear fields
clearFormFields();
container.classList.remove("right-panel-active");
}, 1000);
} else {
showMessage(changePasswordMessage, 'error', response.message);
}
})
.withFailureHandler((error) => {
showMessage(changePasswordMessage, 'error', 'Error resetting password.');
console.error(error);
})
.resetPassword(forgotPasswordEmail, newPassword);
});
// --- Resend Verification Link Handler ---
resendVerificationLink.addEventListener('click', (e) => {
e.preventDefault();
const emailToVerify = document.getElementById('loginEmail').value.trim();
if (!isValidEmail(emailToVerify)) {
showMessage(loginMessage, 'error', 'Please enter a valid email to resend verification.');
return;
}
showMessage(loginMessage, 'neutral', 'Resending verification email...');
google.script.run
.withSuccessHandler((response) => {
if (response.success) {
showMessage(loginMessage, 'success', response.message);
} else {
showMessage(loginMessage, 'error', response.message);
}
})
.withFailureHandler((error) => {
showMessage(loginMessage, 'error', 'Error resending verification email.');
console.error(error);
})
.resendVerificationLink(emailToVerify);
});
// --- Helper Functions ---
function showMessage(element, type, message) {
element.textContent = message;
element.className = `message ${type}`;
}
function clearMessages() {
signupMessage.textContent = '';
signupMessage.className = 'message';
loginMessage.textContent = '';
loginMessage.className = 'message';
}
function clearFormFields() {
document.getElementById('signupName').value = '';
document.getElementById('signupEmail').value = '';
document.getElementById('signupPassword').value = '';
document.getElementById('loginEmail').value = '';
document.getElementById('loginPassword').value = '';
}
function isValidEmail(email) {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
}
function showModal(modalElement) {
modalElement.style.display = 'flex'; // Use flex to center content
}
function hideModal(modalElement) {
modalElement.style.display = 'none';
}
closeButtons.forEach(button => {
button.addEventListener('click', () => {
const modalId = button.dataset.modal;
hideModal(document.getElementById(modalId));
if (modalId === 'otpEntryModal') {
clearInterval(otpCountdownInterval); // Stop OTP timer if modal is closed
}
});
});
// Close modal if user clicks outside of it
window.addEventListener('click', (event) => {
if (event.target === emailRequestModal) {
hideModal(emailRequestModal);
}
if (event.target === otpEntryModal) {
hideModal(otpEntryModal);
clearInterval(otpCountdownInterval);
}
if (event.target === changePasswordModal) {
hideModal(changePasswordModal);
}
});
function startOtpTimer(minutes) {
clearInterval(otpCountdownInterval); // Clear any existing timer
let time = minutes * 60;
otpTimerDisplay.textContent = `OTP expires in ${formatTime(time)}`;
otpCountdownInterval = setInterval(() => {
time--;
otpTimerDisplay.textContent = `OTP expires in ${formatTime(time)}`;
if (time <= 0) {
clearInterval(otpCountdownInterval);
otpTimerDisplay.textContent = 'OTP has expired.';
showMessage(otpMessage, 'error', 'OTP expired. Please resend.');
otpInput.disabled = true; // Disable input if OTP expires
verifyOtpButton.disabled = true;
} else {
otpInput.disabled = false; // Enable input if OTP is active
verifyOtpButton.disabled = false;
}
}, 1000);
}
function formatTime(seconds) {
const min = Math.floor(seconds / 60);
const sec = seconds % 60;
return `${min.toString().padStart(2, '0')}:${sec.toString().padStart(2, '0')}`;
}
</script>