forked from omroy07/AgriTech
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
401 lines (345 loc) · 10.9 KB
/
auth.js
File metadata and controls
401 lines (345 loc) · 10.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
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
// Authentication System for AgriTech
class AuthManager {
constructor() {
this.users = this.loadUsers();
this.currentUser = this.getCurrentUser();
}
// Load users from localStorage
loadUsers() {
try {
const users = localStorage.getItem('agritech_users');
return users ? JSON.parse(users) : [];
} catch (error) {
console.error('Error loading users:', error);
return [];
}
}
// Save users to localStorage
saveUsers() {
try {
localStorage.setItem('agritech_users', JSON.stringify(this.users));
} catch (error) {
console.error('Error saving users:', error);
}
}
// Simple password hashing (in production, use proper bcrypt)
hashPassword(password) {
// Simple hash function for demo - in production use proper bcrypt
let hash = 0;
for (let i = 0; i < password.length; i++) {
const char = password.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // Convert to 32-bit integer
}
return hash.toString();
}
// Validate email format
validateEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
// Validate password strength
validatePassword(password) {
if (password.length < 8) {
return { valid: false, message: 'Password must be at least 8 characters long' };
}
if (!/[a-z]/.test(password)) {
return { valid: false, message: 'Password must contain at least one lowercase letter' };
}
if (!/[A-Z]/.test(password)) {
return { valid: false, message: 'Password must contain at least one uppercase letter' };
}
if (!/\d/.test(password)) {
return { valid: false, message: 'Password must contain at least one number' };
}
return { valid: true, message: 'Password is strong' };
}
// Register new user
register(userData) {
const { role, fullname, email, password } = userData;
// Validate input
if (!role || !fullname || !email || !password) {
return { success: false, message: 'All fields are required' };
}
if (!this.validateEmail(email)) {
return { success: false, message: 'Please enter a valid email address' };
}
const passwordValidation = this.validatePassword(password);
if (!passwordValidation.valid) {
return { success: false, message: passwordValidation.message };
}
// Check if user already exists
const existingUser = this.users.find(user => user.email.toLowerCase() === email.toLowerCase());
if (existingUser) {
return { success: false, message: 'An account with this email already exists' };
}
// Create new user
const newUser = {
id: Date.now().toString(),
role: role,
fullname: fullname.trim(),
email: email.toLowerCase().trim(),
password: this.hashPassword(password),
createdAt: new Date().toISOString(),
isActive: true
};
this.users.push(newUser);
this.saveUsers();
return { success: true, message: 'Account created successfully!', user: newUser };
}
// Login user
login(email, password) {
if (!email || !password) {
return { success: false, message: 'Email and password are required' };
}
if (!this.validateEmail(email)) {
return { success: false, message: 'Please enter a valid email address' };
}
// Find user
const user = this.users.find(u => u.email.toLowerCase() === email.toLowerCase());
if (!user) {
return { success: false, message: 'Invalid email or password' };
}
// Check password
const hashedPassword = this.hashPassword(password);
if (user.password !== hashedPassword) {
return { success: false, message: 'Invalid email or password' };
}
if (!user.isActive) {
return { success: false, message: 'Account is deactivated. Please contact support.' };
}
// Update last login
user.lastLogin = new Date().toISOString();
this.saveUsers();
// Set current user session
this.setCurrentUser(user);
return { success: true, message: 'Login successful!', user: user };
}
// Set current user session
setCurrentUser(user) {
try {
const userSession = {
id: user.id,
email: user.email,
fullname: user.fullname,
role: user.role,
loginTime: new Date().toISOString()
};
localStorage.setItem('agritech_current_user', JSON.stringify(userSession));
this.currentUser = userSession;
} catch (error) {
console.error('Error setting current user:', error);
}
}
// Get current user session
getCurrentUser() {
try {
const user = localStorage.getItem('agritech_current_user');
return user ? JSON.parse(user) : null;
} catch (error) {
console.error('Error getting current user:', error);
return null;
}
}
// Check if user is logged in
isLoggedIn() {
return this.currentUser !== null;
}
// Logout user
logout() {
try {
localStorage.removeItem('agritech_current_user');
this.currentUser = null;
return { success: true, message: 'Logged out successfully' };
} catch (error) {
console.error('Error during logout:', error);
return { success: false, message: 'Error during logout' };
}
}
// Get user by email
getUserByEmail(email) {
return this.users.find(user => user.email.toLowerCase() === email.toLowerCase());
}
// Update user profile
updateUser(userId, updates) {
const userIndex = this.users.findIndex(user => user.id === userId);
if (userIndex === -1) {
return { success: false, message: 'User not found' };
}
// Update user data
this.users[userIndex] = { ...this.users[userIndex], ...updates, updatedAt: new Date().toISOString() };
this.saveUsers();
// Update current session if it's the same user
if (this.currentUser && this.currentUser.id === userId) {
this.setCurrentUser(this.users[userIndex]);
}
return { success: true, message: 'Profile updated successfully', user: this.users[userIndex] };
}
// Get all users (admin function)
getAllUsers() {
return this.users.map(user => ({
id: user.id,
role: user.role,
fullname: user.fullname,
email: user.email,
createdAt: user.createdAt,
lastLogin: user.lastLogin,
isActive: user.isActive
}));
}
// Delete user account
deleteUser(userId) {
const userIndex = this.users.findIndex(user => user.id === userId);
if (userIndex === -1) {
return { success: false, message: 'User not found' };
}
this.users.splice(userIndex, 1);
this.saveUsers();
// Logout if current user is deleted
if (this.currentUser && this.currentUser.id === userId) {
this.logout();
}
return { success: true, message: 'Account deleted successfully' };
}
// Change password
changePassword(userId, currentPassword, newPassword) {
const user = this.users.find(u => u.id === userId);
if (!user) {
return { success: false, message: 'User not found' };
}
// Verify current password
const hashedCurrentPassword = this.hashPassword(currentPassword);
if (user.password !== hashedCurrentPassword) {
return { success: false, message: 'Current password is incorrect' };
}
// Validate new password
const passwordValidation = this.validatePassword(newPassword);
if (!passwordValidation.valid) {
return { success: false, message: passwordValidation.message };
}
// Update password
user.password = this.hashPassword(newPassword);
user.updatedAt = new Date().toISOString();
this.saveUsers();
return { success: true, message: 'Password changed successfully' };
}
// Reset password (simplified - in production, use email verification)
resetPassword(email) {
const user = this.users.find(u => u.email.toLowerCase() === email.toLowerCase());
if (!user) {
return { success: false, message: 'No account found with this email address' };
}
// Generate temporary password
const tempPassword = Math.random().toString(36).slice(-8);
user.password = this.hashPassword(tempPassword);
user.updatedAt = new Date().toISOString();
this.saveUsers();
// In production, send this via email
return {
success: true,
message: 'Password reset successfully',
tempPassword: tempPassword
};
}
}
// Initialize global auth manager
window.authManager = new AuthManager();
// Utility functions for UI
function showAuthMessage(message, type = 'info') {
// Remove existing messages
const existingMessage = document.querySelector('.auth-message');
if (existingMessage) {
existingMessage.remove();
}
const messageDiv = document.createElement('div');
messageDiv.className = `auth-message auth-message-${type}`;
messageDiv.innerHTML = `
<div class="auth-message-content">
<i class="fas fa-${type === 'success' ? 'check-circle' : type === 'error' ? 'exclamation-circle' : 'info-circle'}"></i>
<span>${message}</span>
</div>
`;
// Add styles
messageDiv.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
z-index: 10000;
padding: 15px 20px;
border-radius: 8px;
color: white;
font-weight: 500;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
animation: slideInRight 0.3s ease-out;
max-width: 400px;
`;
if (type === 'success') {
messageDiv.style.background = 'linear-gradient(135deg, #4caf50, #45a049)';
} else if (type === 'error') {
messageDiv.style.background = 'linear-gradient(135deg, #f44336, #e53935)';
} else {
messageDiv.style.background = 'linear-gradient(135deg, #2196f3, #1976d2)';
}
document.body.appendChild(messageDiv);
// Auto remove after 5 seconds
setTimeout(() => {
if (messageDiv.parentNode) {
messageDiv.style.animation = 'slideOutRight 0.3s ease-out';
setTimeout(() => {
if (messageDiv.parentNode) {
messageDiv.remove();
}
}, 300);
}
}, 5000);
}
// Add CSS animations
const authStyles = document.createElement('style');
authStyles.textContent = `
@keyframes slideInRight {
from {
opacity: 0;
transform: translateX(100%);
}
to {
opacity: 1;
transform: translateX(0);
}
}
@keyframes slideOutRight {
from {
opacity: 1;
transform: translateX(0);
}
to {
opacity: 0;
transform: translateX(100%);
}
}
.auth-message-content {
display: flex;
align-items: center;
gap: 10px;
}
.auth-message-content i {
font-size: 1.2rem;
}
`;
document.head.appendChild(authStyles);
// Check authentication on protected pages
function requireAuth() {
if (!window.authManager.isLoggedIn()) {
showAuthMessage('Please log in to access this page', 'error');
setTimeout(() => {
window.location.href = 'login.html';
}, 2000);
return false;
}
return true;
}
// Redirect if already logged in
function redirectIfLoggedIn() {
if (window.authManager.isLoggedIn()) {
window.location.href = 'main.html';
}
}