Spaces:
Running
Currently, my authentication system has an issue:
Browse filesWhen I log out and then log in again, the first login attempt shows βInvalid username or password.β
Then, when I try to register again with the same username, it says βUsername already exists.β
After that, logging in again with the same credentials works.
I want you to fix this issue and build a simple but proper authentication system with the following behavior:
β
Requirements
Users should be able to register (sign up), log in, and log out properly.
When a user logs out, all their session data and chat history should be cleared.
Chat history does not need to persist between sessions β itβs fine if it resets on logout.
Users should still be able to log back in with the same username and password anytime.
Passwords must be securely stored (hashed).
Once logged in, users should be redirected to the chat interface.
Each user will get their OpenRouter API key stored and retrievable after login.
The system should handle authentication and session management cleanly and consistently.
- index.html +53 -29
|
@@ -275,22 +275,35 @@ const settingsBtn = document.getElementById('settingsBtn');
|
|
| 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 |
-
|
| 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
|
|
@@ -298,8 +311,17 @@ const settingsBtn = document.getElementById('settingsBtn');
|
|
| 298 |
chatList.innerHTML = '';
|
| 299 |
currentChatId = null;
|
| 300 |
addMessage('system', 'Please log in to start chatting.');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
}
|
| 302 |
-
|
| 303 |
const username = localStorage.getItem('chatRouterUser');
|
| 304 |
if (!username) return;
|
| 305 |
|
|
@@ -671,38 +693,34 @@ async function sendMessage() {
|
|
| 671 |
authToggleBtn.addEventListener('click', () => {
|
| 672 |
showAuthModal(!isRegistering);
|
| 673 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 674 |
|
| 675 |
-
authActionBtn.addEventListener('click', () => {
|
| 676 |
if (isRegistering) {
|
| 677 |
-
const
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 691 |
} else {
|
| 692 |
-
|
| 693 |
-
|
| 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 |
-
|
| 706 |
newSidebarChat.addEventListener('click', createNewChat);
|
| 707 |
|
| 708 |
// Sidebar toggle for mobile
|
|
@@ -724,9 +742,15 @@ messageInput.addEventListener('keydown', (e) => {
|
|
| 724 |
});
|
| 725 |
// Initialize
|
| 726 |
loadSettings();
|
| 727 |
-
updateAuthUI();
|
| 728 |
|
| 729 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 730 |
setTimeout(() => {
|
| 731 |
if (!localStorage.getItem('chatRouterUser')) {
|
| 732 |
addMessage('system', 'Welcome to ChatRouter! Please log in to start chatting.');
|
|
|
|
| 275 |
function generateChatId() {
|
| 276 |
return 'chat_' + Date.now().toString(36) + Math.random().toString(36).substr(2, 5);
|
| 277 |
}
|
| 278 |
+
async function hashPassword(password) {
|
| 279 |
+
const encoder = new TextEncoder();
|
| 280 |
+
const data = encoder.encode(password);
|
| 281 |
+
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
|
| 282 |
+
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
| 283 |
+
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
|
| 284 |
+
}
|
| 285 |
+
|
| 286 |
+
async function loginUser(username, password) {
|
| 287 |
+
const hashedPassword = await hashPassword(password);
|
| 288 |
+
const storedHash = localStorage.getItem(`chatRouterPass_${username}`);
|
| 289 |
+
|
| 290 |
+
if (storedHash !== hashedPassword) {
|
| 291 |
+
alert('Invalid username or password');
|
| 292 |
+
return false;
|
| 293 |
+
}
|
| 294 |
|
|
|
|
|
|
|
| 295 |
localStorage.setItem('chatRouterUser', username);
|
| 296 |
+
sessionStorage.setItem('chatRouterSession', username);
|
| 297 |
updateAuthUI();
|
| 298 |
|
|
|
|
| 299 |
createNewChat();
|
|
|
|
|
|
|
| 300 |
loadChatList(username);
|
| 301 |
+
return true;
|
| 302 |
}
|
| 303 |
function logoutUser() {
|
| 304 |
const username = localStorage.getItem('chatRouterUser');
|
| 305 |
localStorage.removeItem('chatRouterUser');
|
| 306 |
+
sessionStorage.removeItem('chatRouterSession');
|
| 307 |
updateAuthUI();
|
| 308 |
|
| 309 |
// Clear current chat and sidebar
|
|
|
|
| 311 |
chatList.innerHTML = '';
|
| 312 |
currentChatId = null;
|
| 313 |
addMessage('system', 'Please log in to start chatting.');
|
| 314 |
+
|
| 315 |
+
// Clear all chat history for this session
|
| 316 |
+
for (let i = localStorage.length - 1; i >= 0; i--) {
|
| 317 |
+
const key = localStorage.key(i);
|
| 318 |
+
if (key.startsWith(`chatHistory_${username}_`) ||
|
| 319 |
+
key.startsWith(`chatTitle_${username}_`)) {
|
| 320 |
+
localStorage.removeItem(key);
|
| 321 |
+
}
|
| 322 |
+
}
|
| 323 |
}
|
| 324 |
+
function createNewChat() {
|
| 325 |
const username = localStorage.getItem('chatRouterUser');
|
| 326 |
if (!username) return;
|
| 327 |
|
|
|
|
| 693 |
authToggleBtn.addEventListener('click', () => {
|
| 694 |
showAuthModal(!isRegistering);
|
| 695 |
});
|
| 696 |
+
authActionBtn.addEventListener('click', async () => {
|
| 697 |
+
const username = regUsername.value.trim();
|
| 698 |
+
const password = authPassword.value.trim();
|
| 699 |
+
|
| 700 |
+
if (!username || !password) {
|
| 701 |
+
alert('Please enter both username and password');
|
| 702 |
+
return;
|
| 703 |
+
}
|
| 704 |
|
|
|
|
| 705 |
if (isRegistering) {
|
| 706 |
+
const hashedPassword = await hashPassword(password);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 707 |
|
| 708 |
if (localStorage.getItem(`chatRouterPass_${username}`)) {
|
| 709 |
alert('Username already exists');
|
| 710 |
return;
|
| 711 |
}
|
| 712 |
|
| 713 |
+
localStorage.setItem(`chatRouterPass_${username}`, hashedPassword);
|
| 714 |
+
if (await loginUser(username, password)) {
|
| 715 |
+
authModal.classList.add('hidden');
|
| 716 |
+
}
|
| 717 |
} else {
|
| 718 |
+
if (await loginUser(username, password)) {
|
| 719 |
+
authModal.classList.add('hidden');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 720 |
}
|
|
|
|
|
|
|
| 721 |
}
|
|
|
|
| 722 |
});
|
| 723 |
+
newChatBtn.addEventListener('click', createNewChat);
|
| 724 |
newSidebarChat.addEventListener('click', createNewChat);
|
| 725 |
|
| 726 |
// Sidebar toggle for mobile
|
|
|
|
| 742 |
});
|
| 743 |
// Initialize
|
| 744 |
loadSettings();
|
|
|
|
| 745 |
|
| 746 |
+
// Check for active session
|
| 747 |
+
const activeUser = sessionStorage.getItem('chatRouterSession');
|
| 748 |
+
if (activeUser && localStorage.getItem(`chatRouterPass_${activeUser}`)) {
|
| 749 |
+
localStorage.setItem('chatRouterUser', activeUser);
|
| 750 |
+
}
|
| 751 |
+
|
| 752 |
+
updateAuthUI();
|
| 753 |
+
// Welcome message
|
| 754 |
setTimeout(() => {
|
| 755 |
if (!localStorage.getItem('chatRouterUser')) {
|
| 756 |
addMessage('system', 'Welcome to ChatRouter! Please log in to start chatting.');
|