-
Notifications
You must be signed in to change notification settings - Fork 0
/
registration.html
631 lines (547 loc) · 24 KB
/
registration.html
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create Account - The Give Hub</title>
<link rel="stylesheet" href="login.css">
<link rel="icon" type="image/svg+xml" href="/img/favicon.svg">
</head>
<body>
<div class="login-container">
<div class="login-header">
<img class="logo" src="/img/black-logo.svg">
<div class="login-header-text">
<h1>The Give Hub<br><span style="font-size:0.8em;">Account Registration</span></h1>
<p>Join The Give Hub community</p>
</div>
</div>
<div class="registration-steps">
<div class="step-indicator active" data-step="1">
<div class="step-number">1</div>
<span>Account</span>
</div>
<div class="step-divider"></div>
<div class="step-indicator" data-step="2">
<div class="step-number">2</div>
<span>Profile</span>
</div>
<div class="step-divider"></div>
<div class="step-indicator" data-step="3">
<div class="step-number">3</div>
<span>Verify</span>
</div>
</div>
<form class="login-form" id="registrationForm">
<!-- Step 1: Account Details -->
<div class="registration-step active" data-step="1">
<div class="form-group">
<label for="email">Email</label>
<input type="email" id="email" name="email" required>
<div class="form-error" id="emailError"></div>
</div>
<div class="form-group">
<label for="username">Username</label>
<input type="text" id="username" name="username" required>
<div class="form-error" id="usernameError"></div>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" required>
<div class="password-strength">
<div class="strength-meter">
<div id="strengthBar"></div>
</div>
<div class="strength-text" id="strengthText"></div>
</div>
<div class="requirements">
<div class="requirement" data-requirement="length">
<svg class="check-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="20 6 9 17 4 12"></polyline>
</svg>
<span>At least 8 characters</span>
</div>
<div class="requirement" data-requirement="uppercase">
<svg class="check-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="20 6 9 17 4 12"></polyline>
</svg>
<span>One uppercase letter</span>
</div>
<div class="requirement" data-requirement="number">
<svg class="check-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="20 6 9 17 4 12"></polyline>
</svg>
<span>One number</span>
</div>
<div class="requirement" data-requirement="special">
<svg class="check-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="20 6 9 17 4 12"></polyline>
</svg>
<span>One special character</span>
</div>
</div>
<div class="form-error" id="passwordError"></div>
</div>
<button type="button" class="login-button" onclick="app.nextStep(event)">Continue</button>
<div class="register-prompt">Already have an account? <a href="/login.html" title="Login to The Give Hub">Sign in</a></div>
</div>
<!-- Step 2: Profile Details -->
<div class="registration-step" data-step="2">
<div class="avatar-upload">
<div class="avatar-circle" id="avatarUpload">
<img id="avatarPreview" src="" alt="" style="display: none;">
<div class="upload-icon">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
<polyline points="17 8 12 3 7 8"></polyline>
<line x1="12" y1="3" x2="12" y2="15"></line>
</svg>
</div>
</div>
<div class="avatar-text">Upload profile picture</div>
</div>
<div class="form-group">
<label for="firstName">First Name</label>
<input type="text" id="firstName" name="firstName" required>
<div class="form-error" id="firstNameError"></div>
</div>
<div class="form-group">
<label for="lastName">Last Name</label>
<input type="text" id="lastName" name="lastName" required>
<div class="form-error" id="lastNameError"></div>
</div>
<div class="button-group">
<button type="button" class="login-button back-button" onclick="app.prevStep()">Back</button>
<button type="button" class="login-button" onclick="app.nextStep(event)">Continue</button>
</div>
</div>
<!-- Step 3: Verification -->
<div class="registration-step" data-step="3">
<div class="form-group">
<label for="verificationCode">Verification Code</label>
<input type="text" id="verificationCode" name="verificationCode"
required pattern="[0-9]*" inputmode="numeric" maxlength="6">
<div class="form-error" id="verificationError"></div>
</div>
<p class="verification-instructions"></p>
<div class="resend-section">
<span>Didn't receive the code?</span>
<button type="button" class="resend-button" onclick="app.resendCode()">Resend Code</button>
</div>
<div class="button-group">
<button type="button" class="login-button back-button" onclick="app.prevStep()">
Back
</button>
<button type="submit" class="login-button" id="registerButton">
<div class="spinner"></div>
<span>Complete Registration</span>
</button>
</div>
</div>
<!-- Success Message -->
<div class="registration-step" data-step="success">
<div class="success-message">
<svg class="success-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"></path>
<polyline points="22 4 12 14.01 9 11.01"></polyline>
</svg>
<h2>Registration Complete!</h2>
<p>Your account has been created successfully.</p>
<button type="button" class="login-button" onclick="window.location.href='/login'">
Go to Login
</button>
</div>
</div>
</form>
</div>
<script>
const app = {
currentStep: 1,
elements: {
form: document.getElementById('registrationForm'),
steps: document.querySelectorAll('.registration-step'),
indicators: document.querySelectorAll('.step-indicator'),
password: document.getElementById('password'),
strengthBar: document.getElementById('strengthBar'),
strengthText: document.getElementById('strengthText'),
requirements: document.querySelectorAll('.requirement'),
avatarUpload: document.getElementById('avatarUpload'),
avatarPreview: document.getElementById('avatarPreview'),
registerButton: document.getElementById('registerButton')
},
init() {
this.setupEventListeners();
this.setupPasswordStrength();
this.setupAvatarUpload();
},
setupEventListeners() {
this.elements.form.addEventListener('submit', (e) => this.handleRegistration(e));
},
setupPasswordStrength() {
this.elements.password.addEventListener('input', (e) => {
const password = e.target.value;
const strength = this.calculatePasswordStrength(password);
this.updatePasswordStrength(strength);
this.updateRequirements(password);
});
},
setupAvatarUpload() {
this.elements.avatarUpload.addEventListener('click', () => {
const input = document.createElement('input');
input.type = 'file';
input.accept = 'image/*';
input.onchange = (e) => this.handleAvatarUpload(e);
input.click();
});
},
calculatePasswordStrength(password) {
let score = 0;
// Length
if (password.length >= 8) score++;
if (password.length >= 12) score++;
// Complexity
if (/[A-Z]/.test(password)) score++;
if (/[0-9]/.test(password)) score++;
if (/[^A-Za-z0-9]/.test(password)) score++;
return score;
},
updatePasswordStrength(score) {
const strengthBar = this.elements.strengthBar;
const strengthText = this.elements.strengthText;
let strength = '';
let width = '';
let color = '';
switch (score) {
case 0:
case 1:
strength = 'Weak';
width = '33.33%';
color = 'weak';
break;
case 2:
case 3:
strength = 'Medium';
width = '66.66%';
color = 'medium';
break;
case 4:
case 5:
strength = 'Strong';
width = '100%';
color = 'strong';
break;
}
strengthBar.style.width = width;
strengthBar.className = color;
strengthText.textContent = strength;
},
updateRequirements(password) {
const requirements = {
length: password.length >= 8,
uppercase: /[A-Z]/.test(password),
number: /[0-9]/.test(password),
special: /[^A-Za-z0-9]/.test(password)
};
Object.entries(requirements).forEach(([requirement, isMet]) => {
const element = document.querySelector(`[data-requirement="${requirement}"]`);
if (element) {
element.classList.toggle('met', isMet);
}
});
return Object.values(requirements).every(Boolean);
},
async handleAvatarUpload(event) {
const file = event.target.files[0];
if (!file) return;
if (!file.type.startsWith('image/')) {
this.showError('avatarError', 'Please select an image file');
return;
}
try {
// Show preview
const reader = new FileReader();
reader.onload = (e) => {
this.elements.avatarPreview.src = e.target.result;
this.elements.avatarPreview.style.display = 'block';
};
reader.readAsDataURL(file);
// Upload to server
const formData = new FormData();
formData.append('avatar', file);
formData.append('username', document.querySelector("#username").value);
formData.append('email', document.querySelector("#email").value);
const response = await fetch('/api/auth/upload-avatar', {
method: 'POST',
body: formData
});
const result = await response.json();
if (!result.success) {
throw new Error(result.error || 'Failed to upload avatar');
}
} catch (error) {
this.showError('avatarError', error.message);
}
},
prevStep() {
if (this.currentStep > 1) {
this.currentStep--;
this.updateSteps();
}
},
validateCurrentStep() {
const currentStepElement = document.querySelector(`.registration-step[data-step="${this.currentStep}"]`);
const inputs = currentStepElement.querySelectorAll('input[required]');
let isValid = true;
inputs.forEach(input => {
if (!input.value) {
this.showError(`${input.id}Error`, `${input.name} is required`);
isValid = false;
}
});
// Step-specific validation
if (isValid && this.currentStep === 1) {
// Validate email format
const email = document.getElementById('email').value;
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
this.showError('emailError', 'Please enter a valid email address');
isValid = false;
}
// Validate password strength
const password = document.getElementById('password').value;
if (this.calculatePasswordStrength(password) < 3) {
this.showError('passwordError', 'Please choose a stronger password');
isValid = false;
}
}
return isValid;
},
updateSteps() {
// Update step visibility
this.elements.steps.forEach(step => {
step.classList.toggle('active',
step.dataset.step === this.currentStep.toString());
});
// Update indicators
this.elements.indicators.forEach(indicator => {
const step = parseInt(indicator.dataset.step);
indicator.classList.toggle('active', step === this.currentStep);
indicator.classList.toggle('completed', step < this.currentStep);
});
},
showStep(step) {
this.elements.steps.forEach(s => {
s.classList.toggle('active', s.dataset.step === step);
});
},
showError(elementId, message) {
const element = document.getElementById(elementId);
if (element) {
element.textContent = message;
element.classList.add('active');
}
},
clearError(elementId) {
const element = document.getElementById(elementId);
if (element) {
element.textContent = '';
element.classList.remove('active');
}
},
setLoading(loading) {
this.elements.registerButton.classList.toggle('loading', loading);
this.elements.registerButton.disabled = loading;
},
async requestVerificationCode(email) {
try {
const response = await fetch('/api/auth/request-verification', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ email })
});
const result = await response.json();
if (!result.success) {
throw new Error(result.error || 'Failed to send verification code');
}
return true;
} catch (error) {
this.showError('emailError', error.message);
return false;
}
},
async sendVerificationEmail(email) {
try {
const response = await fetch('/api/auth/send-verification', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ email })
});
const result = await response.json();
if (!result.success) {
throw new Error(result.error || 'Failed to send verification email');
}
return true;
} catch (error) {
console.error('Error sending verification email:', error);
throw error;
}
},
// Add a resend verification code function
async resendVerificationCode() {
const email = document.getElementById('email').value;
if (!email) {
this.showError('verificationError', 'Email address is required');
return;
}
const resendButton = document.querySelector('.resend-button');
if (resendButton) {
resendButton.disabled = true;
}
try {
await this.sendVerificationEmail(email);
// Show success message
const successMessage = document.createElement('div');
successMessage.className = 'success-message';
successMessage.textContent = 'Verification code resent!';
document.querySelector('.verification-instructions').appendChild(successMessage);
// Remove success message after 3 seconds
setTimeout(() => successMessage.remove(), 3000);
} catch (error) {
this.showError('verificationError', error.message);
} finally {
if (resendButton) {
resendButton.disabled = false;
}
}
},
async nextStep(e) {
if (!this.validateCurrentStep()) {
return;
}
if (this.currentStep === 3) {
this.handleRegistration(e);
}
// If moving from step 2 to verification step (step 3)
if (this.currentStep === 2) {
const email = document.getElementById('email').value;
this.setLoading(true);
try {
// Send verification email before showing verification step
await this.sendVerificationEmail(email);
// Update verification instructions
const instructions = document.querySelector('.verification-instructions');
if (instructions) {
instructions.textContent = `We've sent a verification code to ${email}. Please check your inbox and enter the code below.`;
}
// Move to next step
this.currentStep++;
this.updateSteps();
} catch (error) {
this.showError('emailError', error.message);
} finally {
this.setLoading(false);
}
return;
}
// For other steps, just move forward
if (this.currentStep < 3) {
this.currentStep++;
this.updateSteps();
}
},
async verification() {
// First verify the code
const verificationResult = await this.verifyCode(
formData.get('email'),
formData.get('verificationCode')
);
if (!verificationResult.success) {
throw new Error(verificationResult.error || 'Invalid verification code');
}
},
// Modify the existing handleRegistration method to include verification
async handleRegistration(e) {
e.preventDefault();
//if (this.currentStep !== 3) {
// return;
//}
this.setLoading(true);
try {
const formData = new FormData(this.elements.form);
// Then complete registration
const registrationData = {
email: formData.get('email'),
username: formData.get('username'),
password: formData.get('password'),
profile: {
firstName: formData.get('firstName'),
lastName: formData.get('lastName')
}
};
const response = await fetch('/api/auth/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(registrationData)
});
const result = await response.json();
if (result.success) {
this.showStep('success');
} else {
throw new Error(result.error || 'Registration failed');
}
} catch (error) {
this.showError('verificationError', error.message);
} finally {
this.setLoading(false);
}
},
async verifyCode(email, code) {
try {
const response = await fetch('/api/auth/verify-code', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
email,
code
})
});
return await response.json();
} catch (error) {
return {
success: false,
error: error.message
};
}
},
// Add a resend code feature
async resendCode() {
const email = document.getElementById('email').value;
this.setLoading(true);
try {
await this.requestVerificationCode(email);
// Show success message
const message = document.createElement('div');
message.className = 'success-message';
message.textContent = 'Verification code resent!';
document.querySelector('.verification-step').appendChild(message);
// Remove message after 3 seconds
setTimeout(() => message.remove(), 3000);
} catch (error) {
this.showError('verificationError', error.message);
} finally {
this.setLoading(false);
}
}
};
// Initialize the app
document.addEventListener('DOMContentLoaded', () => app.init());
</script>
</body>
</html>