forked from stutitiwari23/Resume-Builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
326 lines (280 loc) · 9.8 KB
/
auth.js
File metadata and controls
326 lines (280 loc) · 9.8 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
// auth.js - Handles simulated authentication, registration, login, logout, and session persistence
document.addEventListener('DOMContentLoaded', function () {
/* =======================
ELEMENT REFERENCES
======================== */
const authSection = document.getElementById('auth-section');
const resumeSection = document.getElementById('resume-section');
const landingSection = document.getElementById('landing-section');
const registerForm = document.getElementById('register');
const loginForm = document.getElementById('login');
const showLogin = document.getElementById('show-login');
const showRegister = document.getElementById('show-register');
const logoutBtn = document.getElementById('logout');
const topLoginBtn = document.getElementById('show-login-top');
const topRegisterBtn = document.getElementById('show-register-top');
const ctaBtn = document.getElementById('cta-create');
const userNameEl = document.getElementById('user-name');
const downloadResumeBtn = document.getElementById('download-resume');
const regCloseBtn = document.querySelector('.register-close-auth');
const loginCloseBtn = document.querySelector('.login-colse-auth');
const nameError = document.getElementById('name-error-message');
const emailError = document.getElementById('email-error-message');
const passwordError = document.getElementById('password-error-message');
/* =======================
SESSION CHECK (SAFE)
======================== */
const currentUser = localStorage.getItem('currentUser');
const userData = currentUser ? getUserData(currentUser) : null;
// Resume Builder page should never be auto-hidden
if (resumeSection) {
resumeSection.classList.remove('hidden');
}
// Download button visibility (safe)
if (downloadResumeBtn) {
if (currentUser && userData?.session) {
downloadResumeBtn.classList.remove('hidden');
} else {
downloadResumeBtn.classList.add('hidden');
}
}
/* =======================
FORM TOGGLE LOGIC
======================== */
if (showLogin) {
showLogin.addEventListener('click', (e) => {
e.preventDefault();
toggleAuthForms('login');
});
}
if (showRegister) {
showRegister.addEventListener('click', (e) => {
e.preventDefault();
toggleAuthForms('register');
});
}
if (topLoginBtn) {
topLoginBtn.addEventListener('click', (e) => {
e.preventDefault();
showAuthSection();
toggleAuthForms('login');
});
}
if (topRegisterBtn) {
topRegisterBtn.addEventListener('click', (e) => {
e.preventDefault();
showAuthSection();
toggleAuthForms('register');
});
}
if (ctaBtn) {
ctaBtn.addEventListener('click', () => {
if (currentUser && userData?.session) {
showResumeSection();
} else {
showAuthSection();
}
});
}
/* =======================
REGISTER
======================== */
if (registerForm) {
registerForm.addEventListener('submit', function (e) {
e.preventDefault();
const fullName = Security.sanitizeInput(document.getElementById('reg-fullname').value.trim());
const email = Security.sanitizeInput(document.getElementById('reg-email').value.trim());
const password = document.getElementById('reg-password').value;
if (!fullName || fullName.length < 2) {
if (nameError) {
nameError.textContent = 'Please enter a valid name';
}
return;
}
if (topLoginBtn) {
topLoginBtn.addEventListener('click', (e) => {
const href = topLoginBtn.getAttribute('href') || '';
// If link points to login.html or register.html (separate pages), allow normal navigation
if (href.includes('login.html') || href.includes('register.html')) {
// Allow normal navigation - don't prevent default
return;
}
// Only prevent default if auth section exists (resume-builder page with modal)
if (authSection) {
e.preventDefault();
showAuthSection();
toggleAuthForms('login');
}
});
}
if (topRegisterBtn) {
topRegisterBtn.addEventListener('click', (e) => {
const href = topRegisterBtn.getAttribute('href') || '';
// If link points to login.html or register.html (separate pages), allow normal navigation
if (href.includes('login.html') || href.includes('register.html')) {
// Allow normal navigation - don't prevent default
return;
}
// Only prevent default if auth section exists (resume-builder page with modal)
if (authSection) {
e.preventDefault();
showAuthSection();
toggleAuthForms('register');
}
});
}
if (ctaBtn) {
ctaBtn.addEventListener('click', () => {
if (currentUser && userData?.session) {
showResumeSection();
} else {
showAuthSection();
}
});
}
if (!Validator.isValidEmail(email)) {
if (emailError) {
emailError.textContent = 'Please enter a valid email';
}
return;
}
if (!Validator.validatePassword(password).isValid) {
const passwordValidation = Security.validatePasswordStrength(password);
if (passwordError) {
passwordError.textContent = passwordValidation.feedback.join('. ');
}
return;
}
if (getUserData(email)) {
ValidationUI.showError('reg-email', 'Email already registered');
return;
}
const newUser = {
fullName,
password,
profile: { fullname: fullName, email },
resume: {},
theme: 'light',
session: false
};
saveUserData(email, newUser);
ValidationUI.showToast('Registration successful! Please login.', 'success');
registerForm.reset();
toggleAuthForms('login');
});
}
/* =======================
LOGIN
======================== */
if (loginForm) {
loginForm.addEventListener('submit', function (e) {
e.preventDefault();
const email = document.getElementById('login-email').value.trim();
const password = document.getElementById('login-password').value;
// Check if client is blocked due to too many failed attempts
const blockStatus = Security.isClientBlocked();
if (blockStatus.blocked) {
const remainingMinutes = Math.ceil(blockStatus.remainingTime / 60000);
ValidationUI.showToast(
`Too many failed attempts. Please try again in ${remainingMinutes} minutes.`,
'error'
);
return;
}
// Sanitize inputs
const sanitizedEmail = Security.sanitizeInput(email);
const sanitizedPassword = Security.sanitizeInput(password);
const user = getUserData(sanitizedEmail);
if (!user || user.password !== sanitizedPassword) {
// Record failed attempt
const rateLimitStatus = Security.recordFailedAttempt(sanitizedEmail, 'Invalid credentials');
if (rateLimitStatus.blocked) {
ValidationUI.showToast(rateLimitStatus.message, 'error');
} else {
ValidationUI.showToast(
`Invalid credentials. ${rateLimitStatus.attemptsRemaining} attempts remaining.`,
'error'
);
}
return;
}
// Successful login
Security.recordSuccessfulLogin(sanitizedEmail);
user.session = true;
saveUserData(sanitizedEmail, user);
localStorage.setItem('currentUser', sanitizedEmail);
ValidationUI.showToast('Login successful!', 'success');
showResumeSection();
});
}
/* =======================
LOGOUT
======================== */
if (logoutBtn) {
logoutBtn.addEventListener('click', () => {
const currentUser = localStorage.getItem('currentUser');
if (currentUser) {
const user = getUserData(currentUser);
if (user) {
user.session = false;
saveUserData(currentUser, user);
}
localStorage.removeItem('currentUser');
}
ValidationUI.showToast('Logged out', 'info');
showLanding();
});
}
/* =======================
CLOSE AUTH MODAL
======================== */
if (regCloseBtn) {
regCloseBtn.addEventListener('click', () => {
authSection?.classList.add('hidden');
registerForm?.reset();
});
}
if (loginCloseBtn) {
loginCloseBtn.addEventListener('click', () => {
authSection?.classList.add('hidden');
loginForm?.reset();
});
}
/* =======================
VIEW HELPERS
======================== */
function toggleAuthForms(type) {
const regForm = document.getElementById('register-form');
const logForm = document.getElementById('login-form');
if (!regForm || !logForm) {
return;
}
if (type === 'login') {
regForm.classList.add('hidden');
logForm.classList.remove('hidden');
} else {
logForm.classList.add('hidden');
regForm.classList.remove('hidden');
}
}
function showLanding() {
landingSection?.classList.remove('hidden');
authSection?.classList.add('hidden');
resumeSection?.classList.add('hidden');
}
function showAuthSection() {
authSection?.classList.remove('hidden');
landingSection?.classList.remove('hidden');
resumeSection?.classList.add('hidden');
}
function showResumeSection() {
resumeSection?.classList.remove('hidden');
authSection?.classList.add('hidden');
landingSection?.classList.add('hidden');
if (userNameEl && currentUser) {
userNameEl.textContent = userData?.fullName || currentUser;
userNameEl.classList.remove('hidden');
}
loadUserData?.();
}
});