-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
322 lines (269 loc) · 8.38 KB
/
auth.js
File metadata and controls
322 lines (269 loc) · 8.38 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
// Authentication JavaScript for EduMorph platform
// DOM Elements
const loginForm = document.getElementById("login-form")
const signupForm = document.getElementById("signup-form")
const passwordInput = document.getElementById("password")
const confirmPasswordInput = document.getElementById("confirmPassword")
const passwordStrength = document.getElementById("password-strength")
// Initialize authentication
document.addEventListener("DOMContentLoaded", () => {
initializeAuthForms()
initializePasswordValidation()
checkExistingAuth()
})
// Initialize authentication forms
function initializeAuthForms() {
if (loginForm) {
loginForm.addEventListener("submit", handleLogin)
}
if (signupForm) {
signupForm.addEventListener("submit", handleSignup)
}
// Social authentication buttons
const googleBtn = document.querySelector(".btn-google")
const facebookBtn = document.querySelector(".btn-facebook")
if (googleBtn) {
googleBtn.addEventListener("click", () => handleSocialAuth("google"))
}
if (facebookBtn) {
facebookBtn.addEventListener("click", () => handleSocialAuth("facebook"))
}
}
// Handle login form submission
function handleLogin(e) {
e.preventDefault()
const formData = new FormData(loginForm)
const email = formData.get("email")
const password = formData.get("password")
const remember = formData.get("remember")
// Validate inputs
if (!email || !password) {
showNotification("Please fill in all fields", "error")
return
}
// Simulate authentication
authenticateUser(email, password, remember)
}
// Handle signup form submission
function handleSignup(e) {
e.preventDefault()
const formData = new FormData(signupForm)
const firstName = formData.get("firstName")
const lastName = formData.get("lastName")
const email = formData.get("email")
const password = formData.get("password")
const confirmPassword = formData.get("confirmPassword")
const role = formData.get("role")
const terms = formData.get("terms")
// Validate inputs
if (!firstName || !lastName || !email || !password || !confirmPassword || !role) {
showNotification("Please fill in all fields", "error")
return
}
if (!terms) {
showNotification("Please accept the terms and conditions", "error")
return
}
if (password !== confirmPassword) {
showNotification("Passwords do not match", "error")
return
}
if (!isPasswordStrong(password)) {
showNotification("Please choose a stronger password", "error")
return
}
// Create new user account
createUserAccount({
firstName,
lastName,
email,
password,
role,
})
}
// Authenticate user
function authenticateUser(email, password, remember = false) {
// Simulate API call delay
const submitBtn = document.querySelector('button[type="submit"]')
const originalText = submitBtn.textContent
submitBtn.textContent = "Signing in..."
submitBtn.disabled = true
setTimeout(() => {
// Check if user exists in localStorage (for demo purposes)
const users = getFromStorage("edumorph_users") || []
const user = users.find((u) => u.email === email)
if (user && user.password === password) {
// Successful login
const sessionData = {
id: user.id,
email: user.email,
firstName: user.firstName,
lastName: user.lastName,
role: user.role,
loginTime: new Date().toISOString(),
}
saveToStorage("edumorph_user", sessionData)
if (remember) {
saveToStorage("edumorph_remember", true)
}
showNotification("Login successful! Redirecting...", "success")
setTimeout(() => {
window.location.href = "dashboard.html"
}, 1500)
} else {
// Failed login
showNotification("Invalid email or password", "error")
submitBtn.textContent = originalText
submitBtn.disabled = false
}
}, 1500)
}
// Create user account
function createUserAccount(userData) {
const submitBtn = document.querySelector('button[type="submit"]')
const originalText = submitBtn.textContent
submitBtn.textContent = "Creating account..."
submitBtn.disabled = true
setTimeout(() => {
// Check if user already exists
const users = getFromStorage("edumorph_users") || []
const existingUser = users.find((u) => u.email === userData.email)
if (existingUser) {
showNotification("An account with this email already exists", "error")
submitBtn.textContent = originalText
submitBtn.disabled = false
return
}
// Create new user
const newUser = {
id: generateId(),
...userData,
createdAt: new Date().toISOString(),
enrolledCourses: [],
completedCourses: [],
preferences: {
theme: "light",
notifications: true,
language: "en",
},
}
users.push(newUser)
saveToStorage("edumorph_users", users)
// Auto-login the new user
const sessionData = {
id: newUser.id,
email: newUser.email,
firstName: newUser.firstName,
lastName: newUser.lastName,
role: newUser.role,
loginTime: new Date().toISOString(),
}
saveToStorage("edumorph_user", sessionData)
showNotification("Account created successfully! Redirecting...", "success")
setTimeout(() => {
window.location.href = "dashboard.html"
}, 1500)
}, 1500)
}
// Handle social authentication
function handleSocialAuth(provider) {
showNotification(`${provider.charAt(0).toUpperCase() + provider.slice(1)} authentication coming soon!`, "info")
}
// Password validation
function initializePasswordValidation() {
if (passwordInput) {
passwordInput.addEventListener("input", function () {
const password = this.value
updatePasswordStrength(password)
})
}
if (confirmPasswordInput) {
confirmPasswordInput.addEventListener("input", function () {
const password = passwordInput.value
const confirmPassword = this.value
if (confirmPassword && password !== confirmPassword) {
this.setCustomValidity("Passwords do not match")
} else {
this.setCustomValidity("")
}
})
}
}
// Update password strength indicator
function updatePasswordStrength(password) {
if (!passwordStrength) return
const strength = calculatePasswordStrength(password)
passwordStrength.className = "password-strength"
if (password.length === 0) {
passwordStrength.style.width = "0%"
return
}
if (strength < 3) {
passwordStrength.classList.add("weak")
passwordStrength.style.width = "33%"
} else if (strength < 5) {
passwordStrength.classList.add("medium")
passwordStrength.style.width = "66%"
} else {
passwordStrength.classList.add("strong")
passwordStrength.style.width = "100%"
}
}
// Calculate password strength
function calculatePasswordStrength(password) {
let strength = 0
if (password.length >= 8) strength++
if (password.match(/[a-z]/)) strength++
if (password.match(/[A-Z]/)) strength++
if (password.match(/[0-9]/)) strength++
if (password.match(/[^a-zA-Z0-9]/)) strength++
return strength
}
// Check if password is strong enough
function isPasswordStrong(password) {
return calculatePasswordStrength(password) >= 3
}
// Check for existing authentication
function checkExistingAuth() {
const user = getFromStorage("edumorph_user")
if (user) {
// User is already logged in, redirect to dashboard
window.location.href = "dashboard.html"
}
}
// Utility functions (imported from main script)
function showNotification(message, type) {
if (window.EduMorph && window.EduMorph.showNotification) {
window.EduMorph.showNotification(message, type)
}
}
function generateId() {
if (window.EduMorph && window.EduMorph.generateId) {
return window.EduMorph.generateId()
}
return Math.random().toString(36).substr(2, 9)
}
function saveToStorage(key, data) {
if (window.EduMorph && window.EduMorph.saveToStorage) {
return window.EduMorph.saveToStorage(key, data)
}
try {
localStorage.setItem(key, JSON.stringify(data))
return true
} catch (error) {
console.error("Error saving to storage:", error)
return false
}
}
function getFromStorage(key) {
if (window.EduMorph && window.EduMorph.getFromStorage) {
return window.EduMorph.getFromStorage(key)
}
try {
const data = localStorage.getItem(key)
return data ? JSON.parse(data) : null
} catch (error) {
console.error("Error reading from storage:", error)
return null
}
}