Mohit0199 commited on
Commit
b1649d0
Β·
verified Β·
1 Parent(s): 1697977

Promote version 119476f to main

Browse files

Promoted commit 119476fc75149f1a9955e6c0afc2f0c2d01d5d70 to main branch

Files changed (1) hide show
  1. index.html +60 -132
index.html CHANGED
@@ -94,27 +94,15 @@
94
  </button>
95
  </div>
96
  <div id="authForm" class="space-y-4">
97
- <div id="registerFields" class="hidden space-y-4">
98
- <div>
99
- <label class="block text-sm font-medium mb-1">Username</label>
100
- <input type="text" id="regUsername" placeholder="Choose a username" class="w-full bg-gray-700 border border-gray-600 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-indigo-500">
101
- </div>
102
- <div>
103
- <label class="block text-sm font-medium mb-1">Email</label>
104
- <input type="email" id="regEmail" placeholder="Your email address" class="w-full bg-gray-700 border border-gray-600 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-indigo-500">
105
- </div>
106
  </div>
107
- <div id="loginFields">
108
- <div>
109
- <label class="block text-sm font-medium mb-1">Username or Email</label>
110
- <input type="text" id="authIdentifier" placeholder="Enter username or email" class="w-full bg-gray-700 border border-gray-600 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-indigo-500">
111
- </div>
112
- <div>
113
- <label class="block text-sm font-medium mb-1">Password</label>
114
- <input type="password" id="authPassword" placeholder="Enter your password" class="w-full bg-gray-700 border border-gray-600 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-indigo-500">
115
- </div>
116
  </div>
117
- <button id="authActionBtn" class="w-full bg-indigo-600 hover:bg-indigo-500 text-white py-2 rounded-lg transition-colors">Login</button>
118
  <div class="text-center text-sm">
119
  <span id="authToggleText">Don't have an account? </span>
120
  <button id="authToggleBtn" class="text-indigo-400 hover:text-indigo-300">Register</button>
@@ -253,38 +241,26 @@ const settingsBtn = document.getElementById('settingsBtn');
253
  // User management
254
  let isRegistering = false;
255
 
256
- async function updateAuthUI() {
257
- try {
258
- const response = await fetch('/api/auth/status');
259
- const data = await response.json();
260
-
261
- if (data.authenticated) {
262
- authBtn.innerHTML = `<i data-feather="log-out" class="w-5 h-5"></i><span>Logout</span>`;
263
- authBtn.dataset.state = 'logout';
264
- document.querySelector('#newChatBtn').disabled = false;
265
- document.querySelector('#settingsBtn').disabled = false;
266
- sendBtn.disabled = false;
267
-
268
- // Load user's chat list
269
- loadChatList(data.username);
270
-
271
- // Create new chat if none exists
272
- if (!currentChatId) {
273
- createNewChat();
274
- }
275
- } else {
276
- authBtn.innerHTML = `<i data-feather="user" class="w-5 h-5"></i><span>Login</span>`;
277
- authBtn.dataset.state = 'login';
278
- document.querySelector('#newChatBtn').disabled = true;
279
- document.querySelector('#settingsBtn').disabled = true;
280
- sendBtn.disabled = true;
281
- }
282
- feather.replace();
283
- } catch (error) {
284
- console.error('Auth status check failed:', error);
285
  }
 
286
  }
287
- function showAuthModal(register = false) {
 
288
  isRegistering = register;
289
  authModalTitle.textContent = register ? 'Register' : 'Login';
290
  authActionBtn.textContent = register ? 'Register' : 'Login';
@@ -299,80 +275,31 @@ function showAuthModal(register = false) {
299
  function generateChatId() {
300
  return 'chat_' + Date.now().toString(36) + Math.random().toString(36).substr(2, 5);
301
  }
302
- async function loginUser(identifier, password) {
303
- try {
304
- const response = await fetch('/api/auth/login', {
305
- method: 'POST',
306
- headers: {
307
- 'Content-Type': 'application/json'
308
- },
309
- body: JSON.stringify({
310
- identifier,
311
- password
312
- })
313
- });
314
-
315
- const data = await response.json();
316
-
317
- if (data.success) {
318
- updateAuthUI();
319
- } else {
320
- alert(data.message || 'Login failed');
321
- }
322
- } catch (error) {
323
- console.error('Login error:', error);
324
- alert('Login failed. Please try again.');
325
- }
326
- }
327
 
328
- async function registerUser(username, email, password) {
329
- try {
330
- const response = await fetch('/api/auth/register', {
331
- method: 'POST',
332
- headers: {
333
- 'Content-Type': 'application/json'
334
- },
335
- body: JSON.stringify({
336
- username,
337
- email,
338
- password
339
- })
340
- });
341
-
342
- const data = await response.json();
343
-
344
- if (data.success) {
345
- updateAuthUI();
346
- } else {
347
- alert(data.message || 'Registration failed');
348
- }
349
- } catch (error) {
350
- console.error('Registration error:', error);
351
- alert('Registration failed. Please try again.');
352
- }
353
  }
354
-
355
- async function logoutUser() {
356
- try {
357
- const response = await fetch('/api/auth/logout', {
358
- method: 'POST'
359
- });
360
-
361
- const data = await response.json();
362
-
363
- if (data.success) {
364
- // Clear current chat and sidebar
365
- chatContainer.innerHTML = '';
366
- chatList.innerHTML = '';
367
- currentChatId = null;
368
- addMessage('system', 'Please log in to start chatting.');
369
- updateAuthUI();
370
- }
371
- } catch (error) {
372
- console.error('Logout error:', error);
373
- }
374
  }
375
- function createNewChat() {
376
  const username = localStorage.getItem('chatRouterUser');
377
  if (!username) return;
378
 
@@ -744,37 +671,38 @@ async function sendMessage() {
744
  authToggleBtn.addEventListener('click', () => {
745
  showAuthModal(!isRegistering);
746
  });
747
- authActionBtn.addEventListener('click', async () => {
 
748
  if (isRegistering) {
749
  const username = regUsername.value.trim();
750
- const email = regEmail.value.trim();
751
  const password = authPassword.value.trim();
752
 
753
- if (!username || !email || !password) {
754
- alert('Please fill all fields');
755
  return;
756
  }
757
 
758
- if (password.length < 6) {
759
- alert('Password must be at least 6 characters');
760
  return;
761
  }
762
 
763
- await registerUser(username, email, password);
764
  } else {
765
- const identifier = authIdentifier.value.trim();
766
  const password = authPassword.value.trim();
 
767
 
768
- if (!identifier || !password) {
769
- alert('Please fill all fields');
770
  return;
771
  }
772
 
773
- await loginUser(identifier, password);
774
  }
775
  authModal.classList.add('hidden');
776
  });
777
- newChatBtn.addEventListener('click', createNewChat);
778
  newSidebarChat.addEventListener('click', createNewChat);
779
 
780
  // Sidebar toggle for mobile
 
94
  </button>
95
  </div>
96
  <div id="authForm" class="space-y-4">
97
+ <div id="registerFields" class="hidden">
98
+ <label class="block text-sm font-medium mb-1">Username</label>
99
+ <input type="text" id="regUsername" placeholder="Choose a username" class="w-full bg-gray-700 border border-gray-600 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-indigo-500">
 
 
 
 
 
 
100
  </div>
101
+ <div>
102
+ <label class="block text-sm font-medium mb-1">Password</label>
103
+ <input type="password" id="authPassword" placeholder="Enter your password" class="w-full bg-gray-700 border border-gray-600 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-indigo-500">
 
 
 
 
 
 
104
  </div>
105
+ <button id="authActionBtn" class="w-full bg-indigo-600 hover:bg-indigo-500 text-white py-2 rounded-lg transition-colors">Login</button>
106
  <div class="text-center text-sm">
107
  <span id="authToggleText">Don't have an account? </span>
108
  <button id="authToggleBtn" class="text-indigo-400 hover:text-indigo-300">Register</button>
 
241
  // User management
242
  let isRegistering = false;
243
 
244
+ function updateAuthUI() {
245
+ const loggedInUser = localStorage.getItem('chatRouterUser');
246
+ if (loggedInUser) {
247
+ authBtn.innerHTML = `<i data-feather="log-out" class="w-5 h-5"></i><span>Logout</span>`;
248
+ authBtn.dataset.state = 'logout';
249
+ document.querySelector('#newChatBtn').disabled = false;
250
+ document.querySelector('#settingsBtn').disabled = false;
251
+ sendBtn.disabled = false;
252
+ } else {
253
+ authBtn.innerHTML = `<i data-feather="user" class="w-5 h-5"></i><span>Login</span>`;
254
+ authBtn.dataset.state = 'login';
255
+ document.querySelector('#newChatBtn').disabled = true;
256
+ document.querySelector('#settingsBtn').disabled = true;
257
+ sendBtn.disabled = true;
258
+ showAuthModal();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
259
  }
260
+ feather.replace();
261
  }
262
+
263
+ function showAuthModal(register = false) {
264
  isRegistering = register;
265
  authModalTitle.textContent = register ? 'Register' : 'Login';
266
  authActionBtn.textContent = register ? 'Register' : 'Login';
 
275
  function generateChatId() {
276
  return 'chat_' + Date.now().toString(36) + Math.random().toString(36).substr(2, 5);
277
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
278
 
279
+ function loginUser(username, password) {
280
+ // Simple auth - store user in localStorage
281
+ localStorage.setItem('chatRouterUser', username);
282
+ localStorage.setItem(`chatRouterPass_${username}`, password); // Not secure for production!
283
+ updateAuthUI();
284
+
285
+ // Create a new chat session
286
+ createNewChat();
287
+
288
+ // Load user's chat list
289
+ loadChatList(username);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
290
  }
291
+ function logoutUser() {
292
+ const username = localStorage.getItem('chatRouterUser');
293
+ localStorage.removeItem('chatRouterUser');
294
+ updateAuthUI();
295
+
296
+ // Clear current chat and sidebar
297
+ chatContainer.innerHTML = '';
298
+ chatList.innerHTML = '';
299
+ currentChatId = null;
300
+ addMessage('system', 'Please log in to start chatting.');
 
 
 
 
 
 
 
 
 
 
301
  }
302
+ function createNewChat() {
303
  const username = localStorage.getItem('chatRouterUser');
304
  if (!username) return;
305
 
 
671
  authToggleBtn.addEventListener('click', () => {
672
  showAuthModal(!isRegistering);
673
  });
674
+
675
+ authActionBtn.addEventListener('click', () => {
676
  if (isRegistering) {
677
  const username = regUsername.value.trim();
 
678
  const password = authPassword.value.trim();
679
 
680
+ if (!username || !password) {
681
+ alert('Please enter both username and password');
682
  return;
683
  }
684
 
685
+ if (localStorage.getItem(`chatRouterPass_${username}`)) {
686
+ alert('Username already exists');
687
  return;
688
  }
689
 
690
+ loginUser(username, password);
691
  } else {
692
+ const username = regUsername.value.trim();
693
  const password = authPassword.value.trim();
694
+ const storedPass = localStorage.getItem(`chatRouterPass_${username}`);
695
 
696
+ if (!storedPass || storedPass !== password) {
697
+ alert('Invalid username or password');
698
  return;
699
  }
700
 
701
+ loginUser(username, password);
702
  }
703
  authModal.classList.add('hidden');
704
  });
705
+ newChatBtn.addEventListener('click', createNewChat);
706
  newSidebarChat.addEventListener('click', createNewChat);
707
 
708
  // Sidebar toggle for mobile