-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.js
More file actions
100 lines (83 loc) · 2.57 KB
/
Copy pathusers.js
File metadata and controls
100 lines (83 loc) · 2.57 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
// users.js - Store for user credentials
const users = [
{
id: 1,
username: "user",
password: "password123",
email: "user@example.com",
isAdmin: false,
createdAt: new Date().toISOString()
},
{
id: 999,
username: "admin",
password: "admin123",
email: "admin@boomcart.com",
isAdmin: true,
createdAt: new Date().toISOString()
}
];
// Get all users
function getAllUsers() {
// In a real application, this would fetch from localStorage or a server
return JSON.parse(localStorage.getItem('boomcart_users')) || users;
}
// Save all users
function saveAllUsers(usersArray) {
localStorage.setItem('boomcart_users', JSON.stringify(usersArray));
}
// Find user by username
function findUserByUsername(username) {
const allUsers = getAllUsers();
return allUsers.find(user => user.username === username);
}
// Add a new user
function addUser(username, password, email) {
const allUsers = getAllUsers();
// Check if username already exists
if (findUserByUsername(username)) {
return { success: false, message: "Username already taken" };
}
// Create new user
const newUser = {
id: allUsers.length > 0 ? Math.max(...allUsers.map(u => u.id)) + 1 : 1,
username,
password,
email,
isAdmin: false,
createdAt: new Date().toISOString()
};
// Add to users array
allUsers.push(newUser);
// Save updated array
saveAllUsers(allUsers);
return { success: true, message: "Registration successful" };
}
// Verify login credentials
function verifyCredentials(username, password) {
const user = findUserByUsername(username);
if (!user) {
return { success: false, message: "User not found" };
}
if (user.password !== password) {
return { success: false, message: "Invalid password" };
}
// Return user data without password for security
const { password: _, ...userData } = user;
return { success: true, message: "Login successful", user: userData };
}
// Initialize users in localStorage if not already present
function initializeUsers() {
if (!localStorage.getItem('boomcart_users')) {
saveAllUsers(users);
}
}
// Initialize on load
initializeUsers();
// Make functions available globally
window.userService = {
getAllUsers,
findUserByUsername,
addUser,
verifyCredentials
};