Spaces:
Running
Running
Promote version 00f18fb to main
Browse filesPromoted commit 00f18fbeb787a0e3441cd498bbc1d9c3a9bb82ba to main branch
- index.html +51 -178
index.html
CHANGED
|
@@ -94,19 +94,9 @@
|
|
| 94 |
</button>
|
| 95 |
</div>
|
| 96 |
<div id="authForm" class="space-y-4">
|
| 97 |
-
<div id="registerFields" class="hidden
|
| 98 |
-
<
|
| 99 |
-
|
| 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="Enter your 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">
|
| 105 |
-
</div>
|
| 106 |
-
</div>
|
| 107 |
-
<div>
|
| 108 |
-
<label class="block text-sm font-medium mb-1" id="authEmailLabel">Email</label>
|
| 109 |
-
<input type="email" id="authEmail" placeholder="Enter your 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">
|
| 110 |
</div>
|
| 111 |
<div>
|
| 112 |
<label class="block text-sm font-medium mb-1">Password</label>
|
|
@@ -117,11 +107,11 @@
|
|
| 117 |
<span id="authToggleText">Don't have an account? </span>
|
| 118 |
<button id="authToggleBtn" class="text-indigo-400 hover:text-indigo-300">Register</button>
|
| 119 |
</div>
|
| 120 |
-
<div id="authError" class="text-red-400 text-sm hidden"></div>
|
| 121 |
</div>
|
| 122 |
</div>
|
| 123 |
</div>
|
| 124 |
-
|
|
|
|
| 125 |
<div id="settingsModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden">
|
| 126 |
<div class="bg-gray-800 rounded-xl p-6 w-full max-w-md">
|
| 127 |
<div class="flex justify-between items-center mb-4">
|
|
@@ -248,39 +238,17 @@ const settingsBtn = document.getElementById('settingsBtn');
|
|
| 248 |
const typingIndicator = document.getElementById('typingIndicator');
|
| 249 |
const modelIndicator = document.getElementById('modelIndicator');
|
| 250 |
const themeBtns = document.querySelectorAll('.theme-btn');
|
| 251 |
-
//
|
| 252 |
-
const authError = document.getElementById('authError');
|
| 253 |
-
const authEmail = document.getElementById('authEmail');
|
| 254 |
-
const regEmail = document.getElementById('regEmail');
|
| 255 |
-
const authEmailLabel = document.getElementById('authEmailLabel');
|
| 256 |
let isRegistering = false;
|
| 257 |
|
| 258 |
-
|
| 259 |
-
let csrfToken = '';
|
| 260 |
-
async function getCsrfToken() {
|
| 261 |
-
try {
|
| 262 |
-
const response = await fetch('/api/csrf-token');
|
| 263 |
-
const data = await response.json();
|
| 264 |
-
csrfToken = data.token;
|
| 265 |
-
} catch (error) {
|
| 266 |
-
console.error('Failed to get CSRF token:', error);
|
| 267 |
-
}
|
| 268 |
-
}
|
| 269 |
-
function updateAuthUI() {
|
| 270 |
const loggedInUser = localStorage.getItem('chatRouterUser');
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
if (loggedInUser && token) {
|
| 274 |
authBtn.innerHTML = `<i data-feather="log-out" class="w-5 h-5"></i><span>Logout</span>`;
|
| 275 |
authBtn.dataset.state = 'logout';
|
| 276 |
document.querySelector('#newChatBtn').disabled = false;
|
| 277 |
document.querySelector('#settingsBtn').disabled = false;
|
| 278 |
sendBtn.disabled = false;
|
| 279 |
-
|
| 280 |
-
// Load user's chat list if not already loaded
|
| 281 |
-
if (chatList.children.length === 0) {
|
| 282 |
-
loadChatList(loggedInUser);
|
| 283 |
-
}
|
| 284 |
} else {
|
| 285 |
authBtn.innerHTML = `<i data-feather="user" class="w-5 h-5"></i><span>Login</span>`;
|
| 286 |
authBtn.dataset.state = 'login';
|
|
@@ -299,131 +267,39 @@ function updateAuthUI() {
|
|
| 299 |
authToggleText.textContent = register ? 'Already have an account? ' : 'Don\'t have an account? ';
|
| 300 |
authToggleBtn.textContent = register ? 'Login' : 'Register';
|
| 301 |
registerFields.classList.toggle('hidden', !register);
|
| 302 |
-
authEmailLabel.textContent = register ? 'Email' : 'Email or Username';
|
| 303 |
authModal.classList.remove('hidden');
|
| 304 |
-
authError.classList.add('hidden');
|
| 305 |
-
authError.textContent = '';
|
| 306 |
}
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
const response = await fetch('/api/auth/login', {
|
| 310 |
-
method: 'POST',
|
| 311 |
-
headers: {
|
| 312 |
-
'Content-Type': 'application/json'
|
| 313 |
-
},
|
| 314 |
-
body: JSON.stringify({
|
| 315 |
-
email,
|
| 316 |
-
username,
|
| 317 |
-
password
|
| 318 |
-
})
|
| 319 |
-
});
|
| 320 |
-
|
| 321 |
-
if (!response.ok) {
|
| 322 |
-
const errorText = await response.text();
|
| 323 |
-
try {
|
| 324 |
-
// Try parsing as JSON first
|
| 325 |
-
const errorData = JSON.parse(errorText);
|
| 326 |
-
throw new Error(errorData.message || 'Login failed');
|
| 327 |
-
} catch (e) {
|
| 328 |
-
// If not JSON, show raw error
|
| 329 |
-
throw new Error(errorText || 'Login failed');
|
| 330 |
-
}
|
| 331 |
-
}
|
| 332 |
-
|
| 333 |
-
const data = await response.json();
|
| 334 |
-
|
| 335 |
-
// Store user session
|
| 336 |
-
localStorage.setItem('chatRouterUser', data.user.username);
|
| 337 |
-
localStorage.setItem('chatRouterToken', data.token);
|
| 338 |
-
localStorage.setItem('chatRouterEmail', data.user.email);
|
| 339 |
-
localStorage.setItem('chatRouterUserId', data.user.id);
|
| 340 |
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
loadChatList(data.user.username);
|
| 344 |
-
authModal.classList.add('hidden');
|
| 345 |
-
|
| 346 |
-
} catch (error) {
|
| 347 |
-
authError.textContent = error.message.replace('Error: ', '').replace('error: ', '');
|
| 348 |
-
authError.classList.remove('hidden');
|
| 349 |
-
}
|
| 350 |
}
|
| 351 |
-
async function registerUser(username, email, password) {
|
| 352 |
-
try {
|
| 353 |
-
const response = await fetch('/api/auth/register', {
|
| 354 |
-
headers: {
|
| 355 |
-
'Content-Type': 'application/json'
|
| 356 |
-
},
|
| 357 |
-
method: 'POST',
|
| 358 |
-
headers: {
|
| 359 |
-
'Content-Type': 'application/json'
|
| 360 |
-
},
|
| 361 |
-
body: JSON.stringify({
|
| 362 |
-
username,
|
| 363 |
-
email,
|
| 364 |
-
password
|
| 365 |
-
})
|
| 366 |
-
});
|
| 367 |
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
authError.classList.remove('hidden');
|
| 380 |
-
}
|
| 381 |
}
|
| 382 |
-
|
| 383 |
function logoutUser() {
|
| 384 |
-
|
| 385 |
localStorage.removeItem('chatRouterUser');
|
| 386 |
-
|
| 387 |
-
localStorage.removeItem('chatRouterEmail');
|
| 388 |
-
localStorage.removeItem('chatRouterUserId');
|
| 389 |
|
| 390 |
// Clear current chat and sidebar
|
| 391 |
chatContainer.innerHTML = '';
|
| 392 |
chatList.innerHTML = '';
|
| 393 |
currentChatId = null;
|
| 394 |
-
|
| 395 |
-
updateAuthUI();
|
| 396 |
addMessage('system', 'Please log in to start chatting.');
|
| 397 |
}
|
| 398 |
-
|
| 399 |
-
const token = localStorage.getItem('chatRouterToken');
|
| 400 |
-
if (!token) return false;
|
| 401 |
-
|
| 402 |
-
try {
|
| 403 |
-
const response = await fetch('/api/auth/verify', {
|
| 404 |
-
method: 'GET',
|
| 405 |
-
headers: {
|
| 406 |
-
'Authorization': `Bearer ${token}`
|
| 407 |
-
}
|
| 408 |
-
});
|
| 409 |
-
|
| 410 |
-
if (response.ok) {
|
| 411 |
-
const data = await response.json();
|
| 412 |
-
return data.valid;
|
| 413 |
-
}
|
| 414 |
-
return false;
|
| 415 |
-
} catch (error) {
|
| 416 |
-
console.error('Auth verification failed:', error);
|
| 417 |
-
return false;
|
| 418 |
-
}
|
| 419 |
-
}
|
| 420 |
-
// Chat management
|
| 421 |
-
let currentChatId = null;
|
| 422 |
-
|
| 423 |
-
function generateChatId() {
|
| 424 |
-
return 'chat_' + Date.now().toString(36) + Math.random().toString(36).substr(2, 5);
|
| 425 |
-
}
|
| 426 |
-
function createNewChat() {
|
| 427 |
const username = localStorage.getItem('chatRouterUser');
|
| 428 |
if (!username) return;
|
| 429 |
|
|
@@ -795,40 +671,38 @@ async function sendMessage() {
|
|
| 795 |
authToggleBtn.addEventListener('click', () => {
|
| 796 |
showAuthModal(!isRegistering);
|
| 797 |
});
|
| 798 |
-
|
| 799 |
-
|
| 800 |
-
if (isRegistering) {
|
| 801 |
const username = regUsername.value.trim();
|
| 802 |
-
const email = regEmail.value.trim();
|
| 803 |
const password = authPassword.value.trim();
|
| 804 |
|
| 805 |
-
if (!username || !
|
| 806 |
-
|
| 807 |
-
authError.classList.remove('hidden');
|
| 808 |
return;
|
| 809 |
}
|
| 810 |
|
| 811 |
-
if (
|
| 812 |
-
|
| 813 |
-
authError.classList.remove('hidden');
|
| 814 |
return;
|
| 815 |
}
|
| 816 |
|
| 817 |
-
|
| 818 |
} else {
|
| 819 |
-
const
|
| 820 |
const password = authPassword.value.trim();
|
|
|
|
| 821 |
|
| 822 |
-
if (!
|
| 823 |
-
|
| 824 |
-
authError.classList.remove('hidden');
|
| 825 |
return;
|
| 826 |
}
|
| 827 |
|
| 828 |
-
|
| 829 |
}
|
|
|
|
| 830 |
});
|
| 831 |
-
newChatBtn.addEventListener('click', createNewChat);
|
| 832 |
newSidebarChat.addEventListener('click', createNewChat);
|
| 833 |
|
| 834 |
// Sidebar toggle for mobile
|
|
@@ -852,19 +726,18 @@ messageInput.addEventListener('keydown', (e) => {
|
|
| 852 |
loadSettings();
|
| 853 |
updateAuthUI();
|
| 854 |
|
| 855 |
-
//
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
if (!
|
|
|
|
|
|
|
| 860 |
createNewChat();
|
| 861 |
}
|
| 862 |
-
}
|
| 863 |
-
|
| 864 |
-
|
| 865 |
-
}, 1000);
|
| 866 |
-
}
|
| 867 |
-
// Close sidebar when clicking outside on mobile
|
| 868 |
document.addEventListener('click', (e) => {
|
| 869 |
if (window.innerWidth < 768 && !sidebar.contains(e.target) && e.target !== sidebarToggle) {
|
| 870 |
sidebar.classList.add('hidden');
|
|
|
|
| 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>
|
|
|
|
| 107 |
<span id="authToggleText">Don't have an account? </span>
|
| 108 |
<button id="authToggleBtn" class="text-indigo-400 hover:text-indigo-300">Register</button>
|
| 109 |
</div>
|
|
|
|
| 110 |
</div>
|
| 111 |
</div>
|
| 112 |
</div>
|
| 113 |
+
|
| 114 |
+
<!-- Settings Modal -->
|
| 115 |
<div id="settingsModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden">
|
| 116 |
<div class="bg-gray-800 rounded-xl p-6 w-full max-w-md">
|
| 117 |
<div class="flex justify-between items-center mb-4">
|
|
|
|
| 238 |
const typingIndicator = document.getElementById('typingIndicator');
|
| 239 |
const modelIndicator = document.getElementById('modelIndicator');
|
| 240 |
const themeBtns = document.querySelectorAll('.theme-btn');
|
| 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';
|
|
|
|
| 267 |
authToggleText.textContent = register ? 'Already have an account? ' : 'Don\'t have an account? ';
|
| 268 |
authToggleBtn.textContent = register ? 'Login' : 'Register';
|
| 269 |
registerFields.classList.toggle('hidden', !register);
|
|
|
|
| 270 |
authModal.classList.remove('hidden');
|
|
|
|
|
|
|
| 271 |
}
|
| 272 |
+
// Chat management
|
| 273 |
+
let currentChatId = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
|
| 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
|
|
|
|
| 726 |
loadSettings();
|
| 727 |
updateAuthUI();
|
| 728 |
|
| 729 |
+
// Welcome message
|
| 730 |
+
setTimeout(() => {
|
| 731 |
+
if (!localStorage.getItem('chatRouterUser')) {
|
| 732 |
+
addMessage('system', 'Welcome to ChatRouter! Please log in to start chatting.');
|
| 733 |
+
} else if (!localStorage.getItem('chatRouterModel')) {
|
| 734 |
+
addMessage('system', 'Please select a free model from settings to begin.');
|
| 735 |
+
} else if (!currentChatId) {
|
| 736 |
createNewChat();
|
| 737 |
}
|
| 738 |
+
}, 1000);
|
| 739 |
+
|
| 740 |
+
// Close sidebar when clicking outside on mobile
|
|
|
|
|
|
|
|
|
|
| 741 |
document.addEventListener('click', (e) => {
|
| 742 |
if (window.innerWidth < 768 && !sidebar.contains(e.target) && e.target !== sidebarToggle) {
|
| 743 |
sidebar.classList.add('hidden');
|