-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathauth.js
More file actions
357 lines (301 loc) · 11.1 KB
/
auth.js
File metadata and controls
357 lines (301 loc) · 11.1 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
// Authentication JavaScript for Login and Signup
// API Base URL
const API_BASE_URL = window.location.origin + '/api/auth';
// Utility Functions
const showError = (message) => {
const errorMessage = document.getElementById('errorMessage');
const errorText = document.getElementById('errorText');
if (errorMessage && errorText) {
errorText.textContent = message;
errorMessage.classList.add('show');
setTimeout(() => {
errorMessage.classList.remove('show');
}, 5000);
}
};
const showSuccess = (message) => {
const successMessage = document.getElementById('successMessage');
const successText = document.getElementById('successText');
if (successMessage && successText) {
successText.textContent = message;
successMessage.classList.add('show');
setTimeout(() => {
successMessage.classList.remove('show');
}, 5000);
}
};
const showFieldError = (fieldId, message) => {
const errorElement = document.getElementById(`${fieldId}Error`);
if (errorElement) {
errorElement.textContent = message;
errorElement.classList.remove('hidden');
}
};
const hideFieldError = (fieldId) => {
const errorElement = document.getElementById(`${fieldId}Error`);
if (errorElement) {
errorElement.classList.add('hidden');
}
};
const clearAllFieldErrors = () => {
const errorElements = document.querySelectorAll('[id$="Error"]');
errorElements.forEach(el => el.classList.add('hidden'));
};
const setLoading = (isLoading, buttonId, textId) => {
const button = document.getElementById(buttonId);
const buttonText = document.getElementById(textId);
const spinner = button.querySelector('.spinner');
if (isLoading) {
button.disabled = true;
button.classList.add('opacity-75', 'cursor-not-allowed');
buttonText.textContent = 'Processing...';
spinner.classList.add('show');
} else {
button.disabled = false;
button.classList.remove('opacity-75', 'cursor-not-allowed');
buttonText.textContent = buttonId === 'loginBtn' ? 'Sign In' : 'Create Account';
spinner.classList.remove('show');
}
};
// Email Validation
const isValidEmail = (email) => {
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return re.test(email);
};
// Password Strength Checker
const checkPasswordStrength = (password) => {
let strength = 0;
const strengthBar = document.getElementById('passwordStrengthBar');
const strengthText = document.getElementById('passwordStrengthText');
if (!strengthBar || !strengthText) return;
// Check password criteria
if (password.length >= 6) strength++;
if (password.length >= 10) strength++;
if (/[a-z]/.test(password) && /[A-Z]/.test(password)) strength++;
if (/\d/.test(password)) strength++;
if (/[^a-zA-Z\d]/.test(password)) strength++;
// Update UI based on strength
strengthBar.className = 'password-strength-bar';
if (strength < 2) {
strengthBar.classList.add('password-strength-weak');
strengthText.textContent = 'Weak password';
strengthText.className = 'text-xs text-red-400 mt-1';
} else if (strength < 4) {
strengthBar.classList.add('password-strength-medium');
strengthText.textContent = 'Medium password';
strengthText.className = 'text-xs text-yellow-400 mt-1';
} else {
strengthBar.classList.add('password-strength-strong');
strengthText.textContent = 'Strong password';
strengthText.className = 'text-xs text-green-400 mt-1';
}
};
// Token Management
const saveToken = (token) => {
localStorage.setItem('authToken', token);
};
const getToken = () => {
return localStorage.getItem('authToken');
};
const removeToken = () => {
localStorage.removeItem('authToken');
};
const saveUser = (user) => {
localStorage.setItem('userData', JSON.stringify(user));
};
const getUser = () => {
const user = localStorage.getItem('userData');
return user ? JSON.parse(user) : null;
};
const removeUser = () => {
localStorage.removeItem('userData');
};
// Login Form Handler
const loginForm = document.getElementById('loginForm');
if (loginForm) {
loginForm.addEventListener('submit', async (e) => {
e.preventDefault();
clearAllFieldErrors();
const email = document.getElementById('email').value.trim();
const password = document.getElementById('password').value;
// Client-side validation
let isValid = true;
if (!email) {
showFieldError('email', 'Email is required');
isValid = false;
} else if (!isValidEmail(email)) {
showFieldError('email', 'Please enter a valid email');
isValid = false;
}
if (!password) {
showFieldError('password', 'Password is required');
isValid = false;
}
if (!isValid) return;
// Make API request
setLoading(true, 'loginBtn', 'loginBtnText');
try {
const response = await fetch(`${API_BASE_URL}/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email, password }),
});
const data = await response.json();
if (response.ok && data.success) {
// Save token and user data
saveToken(data.token);
saveUser(data.user);
showSuccess('Login successful! Redirecting...');
// Redirect to home page after 1.5 seconds
setTimeout(() => {
window.location.href = '/';
}, 1500);
} else {
showError(data.message || 'Login failed. Please try again.');
}
} catch (error) {
console.error('Login error:', error);
showError('An error occurred. Please try again later.');
} finally {
setLoading(false, 'loginBtn', 'loginBtnText');
}
});
// Real-time validation
document.getElementById('email').addEventListener('blur', (e) => {
const email = e.target.value.trim();
if (email && !isValidEmail(email)) {
showFieldError('email', 'Please enter a valid email');
} else {
hideFieldError('email');
}
});
}
// Signup Form Handler
const signupForm = document.getElementById('signupForm');
if (signupForm) {
signupForm.addEventListener('submit', async (e) => {
e.preventDefault();
clearAllFieldErrors();
const name = document.getElementById('name').value.trim();
const email = document.getElementById('email').value.trim();
const password = document.getElementById('password').value;
const confirmPassword = document.getElementById('confirmPassword').value;
const termsAccepted = document.getElementById('terms').checked;
// Client-side validation
let isValid = true;
if (!name) {
showFieldError('name', 'Name is required');
isValid = false;
} else if (name.length < 2) {
showFieldError('name', 'Name must be at least 2 characters');
isValid = false;
}
if (!email) {
showFieldError('email', 'Email is required');
isValid = false;
} else if (!isValidEmail(email)) {
showFieldError('email', 'Please enter a valid email');
isValid = false;
}
if (!password) {
showFieldError('password', 'Password is required');
isValid = false;
} else if (password.length < 6) {
showFieldError('password', 'Password must be at least 6 characters');
isValid = false;
}
if (!confirmPassword) {
showFieldError('confirmPassword', 'Please confirm your password');
isValid = false;
} else if (password !== confirmPassword) {
showFieldError('confirmPassword', 'Passwords do not match');
isValid = false;
}
if (!termsAccepted) {
showError('Please accept the Terms of Service and Privacy Policy');
isValid = false;
}
if (!isValid) return;
// Make API request
setLoading(true, 'signupBtn', 'signupBtnText');
try {
const response = await fetch(`${API_BASE_URL}/signup`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ name, email, password }),
});
const data = await response.json();
if (response.ok && data.success) {
// Save token and user data
saveToken(data.token);
saveUser(data.user);
showSuccess('Account created successfully! Redirecting...');
// Redirect to home page after 1.5 seconds
setTimeout(() => {
window.location.href = '/';
}, 1500);
} else {
showError(data.message || 'Signup failed. Please try again.');
}
} catch (error) {
console.error('Signup error:', error);
showError('An error occurred. Please try again later.');
} finally {
setLoading(false, 'signupBtn', 'signupBtnText');
}
});
// Real-time validation
document.getElementById('email').addEventListener('blur', (e) => {
const email = e.target.value.trim();
if (email && !isValidEmail(email)) {
showFieldError('email', 'Please enter a valid email');
} else {
hideFieldError('email');
}
});
document.getElementById('password').addEventListener('input', (e) => {
checkPasswordStrength(e.target.value);
});
document.getElementById('confirmPassword').addEventListener('blur', (e) => {
const password = document.getElementById('password').value;
const confirmPassword = e.target.value;
if (confirmPassword && password !== confirmPassword) {
showFieldError('confirmPassword', 'Passwords do not match');
} else {
hideFieldError('confirmPassword');
}
});
}
// Check if user is logged in and update UI
const updateNavigation = () => {
const user = getUser();
const token = getToken();
if (user && token) {
// User is logged in
console.log('User is logged in:', user);
// You can update navigation here to show user profile, logout button, etc.
}
};
// Initialize
document.addEventListener('DOMContentLoaded', () => {
updateNavigation();
});
// Logout function (can be called from other pages)
const logout = () => {
removeToken();
removeUser();
window.location.href = '/login';
};
// Export functions for use in other scripts
if (typeof window !== 'undefined') {
window.authUtils = {
getToken,
getUser,
logout,
isAuthenticated: () => !!getToken()
};
}