Mhdeusi commited on
Commit
718cfe1
·
verified ·
1 Parent(s): 67514ba

Update js/auth/authentication.js

Browse files
Files changed (1) hide show
  1. js/auth/authentication.js +27 -17
js/auth/authentication.js CHANGED
@@ -171,28 +171,38 @@ class Authentication {
171
  }
172
 
173
  loginUser(username, password) {
174
- const users = this.getUsers();
175
- const hashedPassword = this.hashPassword(password);
176
-
177
- for (const userId in users) {
178
- const user = users[userId];
179
- if (user.username === username && user.password === hashedPassword && user.isActive) {
180
- // ایجاد توکن
181
- const token = this.generateToken(user.userId, user.username, user.role);
182
- this.setToken(token);
183
- this.setCurrentUser(user);
 
 
 
 
 
 
 
 
 
 
 
184
 
185
- // به‌روزرسانی آخرین ورود
186
- user.lastLogin = new Date().toISOString();
187
- this.saveUsers(users);
188
 
189
- return { success: true, user: user, token: token };
190
- }
191
  }
192
-
193
- return { success: false, message: 'Invalid username or password' };
194
  }
195
 
 
 
 
196
  updateUserProgress(userId, score = 0, completedLesson = null) {
197
  const users = this.getUsers();
198
  if (users[userId]) {
 
171
  }
172
 
173
  loginUser(username, password) {
174
+ const users = this.getUsers();
175
+ const hashedPassword = this.hashPassword(password);
176
+ console.log('🔐 Login attempt:', { username, hashedPassword }); // برای دیباگ
177
+
178
+ for (const userId in users) {
179
+ const user = users[userId];
180
+ console.log('📋 User in DB:', {
181
+ username: user.username,
182
+ password: user.password,
183
+ isActive: user.isActive
184
+ }); // برای دیباگ
185
+
186
+ if (user.username === username && user.password === hashedPassword && user.isActive) {
187
+ // ایجاد توکن
188
+ const token = this.generateToken(user.userId, user.username, user.role);
189
+ this.setToken(token);
190
+ this.setCurrentUser(user);
191
+
192
+ // به‌روزرسانی آخرین ورود
193
+ user.lastLogin = new Date().toISOString();
194
+ this.saveUsers(users);
195
 
196
+ // انتشار رویداد تغییر وضعیت
197
+ document.dispatchEvent(new CustomEvent('authStateChanged'));
 
198
 
199
+ return { success: true, user: user, token: token };
 
200
  }
 
 
201
  }
202
 
203
+ return { success: false, message: 'نام کاربری یا رمز عبور اشتباه است' };
204
+ }
205
+
206
  updateUserProgress(userId, score = 0, completedLesson = null) {
207
  const users = this.getUsers();
208
  if (users[userId]) {