Fixed API endpoints
This commit is contained in:
+6
-6
@@ -103,7 +103,7 @@ function setLoadingState(loading) {
|
||||
}
|
||||
|
||||
async function checkSession() {
|
||||
const res = await get('/api/session');
|
||||
const res = await get('/session');
|
||||
if (res.user) {
|
||||
authDiv.style.display = "none";
|
||||
chatDiv.style.display = "grid";
|
||||
@@ -115,7 +115,7 @@ async function checkSession() {
|
||||
}
|
||||
|
||||
async function loadServerChat() {
|
||||
const res = await get("/api/chat/history");
|
||||
const res = await get("/chat/history");
|
||||
if (res.history && res.history.length) {
|
||||
messagesDiv.innerHTML = '';
|
||||
res.history.forEach(msg => {
|
||||
@@ -141,7 +141,7 @@ async function handleAuth(endpoint) {
|
||||
registerBtn.disabled = true;
|
||||
setAuthMessage("Processing...", false);
|
||||
|
||||
const res = await post(`/api/${endpoint}`, {username, password});
|
||||
const res = await post(`/${endpoint}`, {username, password});
|
||||
|
||||
loginBtn.disabled = false;
|
||||
registerBtn.disabled = false;
|
||||
@@ -172,7 +172,7 @@ async function sendMessage() {
|
||||
const loadingMsg = addMessage('', false, true, false);
|
||||
try {
|
||||
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)) {
|
||||
messagesDiv.removeChild(loadingMsg);
|
||||
}
|
||||
@@ -200,7 +200,7 @@ passwordInput.addEventListener("keydown", (e) => {
|
||||
});
|
||||
|
||||
document.getElementById("logout").onclick = async () => {
|
||||
await post("/api/logout", {});
|
||||
await post("/logout", {});
|
||||
chatDiv.style.display = "none";
|
||||
authDiv.style.display = "grid";
|
||||
clearChatHistory();
|
||||
@@ -219,7 +219,7 @@ msgInput.addEventListener("keydown", (e) => {
|
||||
});
|
||||
|
||||
document.getElementById("clearChat").onclick = async () => {
|
||||
await post("/api/chat/clear", {});
|
||||
await post("/chat/clear", {});
|
||||
clearChatHistory();
|
||||
};
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ if (!fs.existsSync(DATA_PATH)) fs.writeFileSync(DATA_PATH, "{}");
|
||||
const users = JSON.parse(fs.readFileSync(DATA_PATH));
|
||||
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;
|
||||
if (users[username]) return res.status(400).json({error: "User already exists"});
|
||||
const hashed = await bcrypt.hash(password, 10);
|
||||
@@ -45,7 +45,7 @@ app.post("/api/register", async (req, res) => {
|
||||
res.json({message: "Registration successful"});
|
||||
});
|
||||
|
||||
app.post("/api/login", async (req, res) => {
|
||||
app.post("/login", async (req, res) => {
|
||||
const {username, password} = req.body;
|
||||
const user = users[username];
|
||||
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"});
|
||||
});
|
||||
|
||||
app.post("/api/logout", (req, res) => {
|
||||
app.post("/logout", (req, res) => {
|
||||
req.session.destroy(() => res.json({message: "Logged out"}));
|
||||
});
|
||||
|
||||
@@ -64,7 +64,7 @@ const requireLogin = (req, res, next) => {
|
||||
next();
|
||||
};
|
||||
|
||||
app.post("/api/chat", requireLogin, async (req, res) => {
|
||||
app.post("/chat", requireLogin, async (req, res) => {
|
||||
const user = req.session.user;
|
||||
const { message, model } = req.body;
|
||||
const selectedModel = model || "mistral:latest";
|
||||
@@ -97,7 +97,7 @@ app.post("/api/chat", requireLogin, async (req, res) => {
|
||||
});
|
||||
|
||||
async function checkSession() {
|
||||
const res = await get('/api/session');
|
||||
const res = await get('/session');
|
||||
if (res.user) {
|
||||
authDiv.style.display = "none";
|
||||
chatDiv.style.display = "grid";
|
||||
@@ -108,17 +108,17 @@ async function checkSession() {
|
||||
return false;
|
||||
}
|
||||
|
||||
app.get("/api/session", (req, res) => {
|
||||
app.get("/session", (req, res) => {
|
||||
if (req.session.user) res.json({user: req.session.user});
|
||||
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;
|
||||
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;
|
||||
chats[user] = [];
|
||||
saveChats();
|
||||
|
||||
Reference in New Issue
Block a user