Fixed API endpoints

This commit is contained in:
2025-10-29 09:15:35 +01:00
parent 1c4ebb484a
commit 6a30a00d45
2 changed files with 14 additions and 14 deletions
+6 -6
View File
@@ -103,7 +103,7 @@ function setLoadingState(loading) {
} }
async function checkSession() { async function checkSession() {
const res = await get('/api/session'); const res = await get('/session');
if (res.user) { if (res.user) {
authDiv.style.display = "none"; authDiv.style.display = "none";
chatDiv.style.display = "grid"; chatDiv.style.display = "grid";
@@ -115,7 +115,7 @@ async function checkSession() {
} }
async function loadServerChat() { async function loadServerChat() {
const res = await get("/api/chat/history"); const res = await get("/chat/history");
if (res.history && res.history.length) { if (res.history && res.history.length) {
messagesDiv.innerHTML = ''; messagesDiv.innerHTML = '';
res.history.forEach(msg => { res.history.forEach(msg => {
@@ -141,7 +141,7 @@ async function handleAuth(endpoint) {
registerBtn.disabled = true; registerBtn.disabled = true;
setAuthMessage("Processing...", false); setAuthMessage("Processing...", false);
const res = await post(`/api/${endpoint}`, {username, password}); const res = await post(`/${endpoint}`, {username, password});
loginBtn.disabled = false; loginBtn.disabled = false;
registerBtn.disabled = false; registerBtn.disabled = false;
@@ -172,7 +172,7 @@ async function sendMessage() {
const loadingMsg = addMessage('', false, true, false); const loadingMsg = addMessage('', false, true, false);
try { try {
const model = document.getElementById("model").value; const model = document.getElementById("model").value;
const res = await post("/api/chat", { message: msg, model }); const res = await post("/chat", { message: msg, model });
if (messagesDiv.contains(loadingMsg)) { if (messagesDiv.contains(loadingMsg)) {
messagesDiv.removeChild(loadingMsg); messagesDiv.removeChild(loadingMsg);
} }
@@ -200,7 +200,7 @@ passwordInput.addEventListener("keydown", (e) => {
}); });
document.getElementById("logout").onclick = async () => { document.getElementById("logout").onclick = async () => {
await post("/api/logout", {}); await post("/logout", {});
chatDiv.style.display = "none"; chatDiv.style.display = "none";
authDiv.style.display = "grid"; authDiv.style.display = "grid";
clearChatHistory(); clearChatHistory();
@@ -219,7 +219,7 @@ msgInput.addEventListener("keydown", (e) => {
}); });
document.getElementById("clearChat").onclick = async () => { document.getElementById("clearChat").onclick = async () => {
await post("/api/chat/clear", {}); await post("/chat/clear", {});
clearChatHistory(); clearChatHistory();
}; };
+8 -8
View File
@@ -36,7 +36,7 @@ if (!fs.existsSync(DATA_PATH)) fs.writeFileSync(DATA_PATH, "{}");
const users = JSON.parse(fs.readFileSync(DATA_PATH)); const users = JSON.parse(fs.readFileSync(DATA_PATH));
const saveUsers = () => fs.writeFileSync(DATA_PATH, JSON.stringify(users, null, 2)); const saveUsers = () => fs.writeFileSync(DATA_PATH, JSON.stringify(users, null, 2));
app.post("/api/register", async (req, res) => { app.post("/register", async (req, res) => {
const {username, password} = req.body; const {username, password} = req.body;
if (users[username]) return res.status(400).json({error: "User already exists"}); if (users[username]) return res.status(400).json({error: "User already exists"});
const hashed = await bcrypt.hash(password, 10); const hashed = await bcrypt.hash(password, 10);
@@ -45,7 +45,7 @@ app.post("/api/register", async (req, res) => {
res.json({message: "Registration successful"}); res.json({message: "Registration successful"});
}); });
app.post("/api/login", async (req, res) => { app.post("/login", async (req, res) => {
const {username, password} = req.body; const {username, password} = req.body;
const user = users[username]; const user = users[username];
if (!user) return res.status(400).json({error: "User not found"}); if (!user) return res.status(400).json({error: "User not found"});
@@ -55,7 +55,7 @@ app.post("/api/login", async (req, res) => {
res.json({message: "Login successful"}); res.json({message: "Login successful"});
}); });
app.post("/api/logout", (req, res) => { app.post("/logout", (req, res) => {
req.session.destroy(() => res.json({message: "Logged out"})); req.session.destroy(() => res.json({message: "Logged out"}));
}); });
@@ -64,7 +64,7 @@ const requireLogin = (req, res, next) => {
next(); next();
}; };
app.post("/api/chat", requireLogin, async (req, res) => { app.post("/chat", requireLogin, async (req, res) => {
const user = req.session.user; const user = req.session.user;
const { message, model } = req.body; const { message, model } = req.body;
const selectedModel = model || "mistral:latest"; const selectedModel = model || "mistral:latest";
@@ -97,7 +97,7 @@ app.post("/api/chat", requireLogin, async (req, res) => {
}); });
async function checkSession() { async function checkSession() {
const res = await get('/api/session'); const res = await get('/session');
if (res.user) { if (res.user) {
authDiv.style.display = "none"; authDiv.style.display = "none";
chatDiv.style.display = "grid"; chatDiv.style.display = "grid";
@@ -108,17 +108,17 @@ async function checkSession() {
return false; return false;
} }
app.get("/api/session", (req, res) => { app.get("/session", (req, res) => {
if (req.session.user) res.json({user: req.session.user}); if (req.session.user) res.json({user: req.session.user});
else res.json({user: null}); else res.json({user: null});
}); });
app.get("/api/chat/history", requireLogin, (req, res) => { app.get("/chat/history", requireLogin, (req, res) => {
const user = req.session.user; const user = req.session.user;
res.json({history: chats[user] || []}); res.json({history: chats[user] || []});
}); });
app.post("/api/chat/clear", requireLogin, (req, res) => { app.post("/chat/clear", requireLogin, (req, res) => {
const user = req.session.user; const user = req.session.user;
chats[user] = []; chats[user] = [];
saveChats(); saveChats();