Spaces:
Sleeping
Sleeping
add new user
Browse files- src/App.js +130 -2
- src/api.js +60 -0
src/App.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
// src/App.js
|
| 2 |
|
| 3 |
import React, { useState } from "react";
|
| 4 |
-
import { sendChat, login, streamChat, uploadPrologFile, getKBMetadata, queryProlog } from "./api";
|
| 5 |
import "./App.css";
|
| 6 |
|
| 7 |
import ReactMarkdown from "react-markdown";
|
|
@@ -28,6 +28,7 @@ function App() {
|
|
| 28 |
// ---------------------------
|
| 29 |
const [token, setToken] = useState(null);
|
| 30 |
const [authError, setAuthError] = useState(null);
|
|
|
|
| 31 |
const [loginForm, setLoginForm] = useState({
|
| 32 |
username: "",
|
| 33 |
password: "",
|
|
@@ -53,6 +54,17 @@ function App() {
|
|
| 53 |
const [kbMetadata, setKbMetadata] = useState(null);
|
| 54 |
const [showUpload, setShowUpload] = useState(false);
|
| 55 |
const [showQueryPanel, setShowQueryPanel] = useState(false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
// ---------------------------
|
| 58 |
// Direct Query State
|
|
@@ -69,6 +81,34 @@ function App() {
|
|
| 69 |
}, [messages]);
|
| 70 |
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
// ============================================================
|
| 73 |
// LOGIN HANDLER
|
| 74 |
// ============================================================
|
|
@@ -85,6 +125,29 @@ function App() {
|
|
| 85 |
}
|
| 86 |
}
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
// ============================================================
|
| 89 |
// FILE UPLOAD HANDLER
|
| 90 |
// ============================================================
|
|
@@ -269,9 +332,28 @@ function App() {
|
|
| 269 |
🔍 Query KB
|
| 270 |
</button>
|
| 271 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
<button
|
| 273 |
className="logout-btn"
|
| 274 |
-
onClick={() =>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
>
|
| 276 |
Logout
|
| 277 |
</button>
|
|
@@ -374,6 +456,52 @@ function App() {
|
|
| 374 |
)}
|
| 375 |
</aside>
|
| 376 |
)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
</div>
|
| 378 |
</div>
|
| 379 |
);
|
|
|
|
| 1 |
// src/App.js
|
| 2 |
|
| 3 |
import React, { useState } from "react";
|
| 4 |
+
import { sendChat, login, streamChat, uploadPrologFile, getKBMetadata, queryProlog, getMe, registerUser } from "./api";
|
| 5 |
import "./App.css";
|
| 6 |
|
| 7 |
import ReactMarkdown from "react-markdown";
|
|
|
|
| 28 |
// ---------------------------
|
| 29 |
const [token, setToken] = useState(null);
|
| 30 |
const [authError, setAuthError] = useState(null);
|
| 31 |
+
const [me, setMe] = useState(null);
|
| 32 |
const [loginForm, setLoginForm] = useState({
|
| 33 |
username: "",
|
| 34 |
password: "",
|
|
|
|
| 54 |
const [kbMetadata, setKbMetadata] = useState(null);
|
| 55 |
const [showUpload, setShowUpload] = useState(false);
|
| 56 |
const [showQueryPanel, setShowQueryPanel] = useState(false);
|
| 57 |
+
const [showRegisterPanel, setShowRegisterPanel] = useState(false);
|
| 58 |
+
|
| 59 |
+
// ---------------------------
|
| 60 |
+
// Admin Register State
|
| 61 |
+
// ---------------------------
|
| 62 |
+
const [registerForm, setRegisterForm] = useState({
|
| 63 |
+
username: "",
|
| 64 |
+
password: "",
|
| 65 |
+
overwrite: false,
|
| 66 |
+
});
|
| 67 |
+
const [registerStatus, setRegisterStatus] = useState(null);
|
| 68 |
|
| 69 |
// ---------------------------
|
| 70 |
// Direct Query State
|
|
|
|
| 81 |
}, [messages]);
|
| 82 |
|
| 83 |
|
| 84 |
+
// ============================================================
|
| 85 |
+
// LOAD CURRENT USER INFO (after login / token change)
|
| 86 |
+
// ============================================================
|
| 87 |
+
React.useEffect(() => {
|
| 88 |
+
let cancelled = false;
|
| 89 |
+
|
| 90 |
+
async function loadMe() {
|
| 91 |
+
if (!token) {
|
| 92 |
+
setMe(null);
|
| 93 |
+
return;
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
try {
|
| 97 |
+
const res = await getMe(token);
|
| 98 |
+
if (!cancelled) setMe(res);
|
| 99 |
+
} catch {
|
| 100 |
+
// If token becomes invalid, leave UI working; API calls will fail.
|
| 101 |
+
if (!cancelled) setMe(null);
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
loadMe();
|
| 106 |
+
return () => {
|
| 107 |
+
cancelled = true;
|
| 108 |
+
};
|
| 109 |
+
}, [token]);
|
| 110 |
+
|
| 111 |
+
|
| 112 |
// ============================================================
|
| 113 |
// LOGIN HANDLER
|
| 114 |
// ============================================================
|
|
|
|
| 125 |
}
|
| 126 |
}
|
| 127 |
|
| 128 |
+
// ============================================================
|
| 129 |
+
// REGISTER USER HANDLER (ADMIN)
|
| 130 |
+
// ============================================================
|
| 131 |
+
async function handleRegisterUser(e) {
|
| 132 |
+
e.preventDefault();
|
| 133 |
+
setRegisterStatus(null);
|
| 134 |
+
|
| 135 |
+
const username = (registerForm.username || "").trim();
|
| 136 |
+
const password = (registerForm.password || "").trim();
|
| 137 |
+
if (!username || !password) {
|
| 138 |
+
setRegisterStatus("Error: Missing username or password");
|
| 139 |
+
return;
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
try {
|
| 143 |
+
const result = await registerUser(username, password, token, registerForm.overwrite);
|
| 144 |
+
setRegisterStatus(`Created user: ${result.username}`);
|
| 145 |
+
setRegisterForm({ username: "", password: "", overwrite: false });
|
| 146 |
+
} catch (err) {
|
| 147 |
+
setRegisterStatus(`Error: ${err.message}`);
|
| 148 |
+
}
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
// ============================================================
|
| 152 |
// FILE UPLOAD HANDLER
|
| 153 |
// ============================================================
|
|
|
|
| 332 |
🔍 Query KB
|
| 333 |
</button>
|
| 334 |
|
| 335 |
+
{me && me.is_admin && (
|
| 336 |
+
<button
|
| 337 |
+
className="btn-secondary"
|
| 338 |
+
onClick={() => {
|
| 339 |
+
setShowRegisterPanel((v) => !v);
|
| 340 |
+
setShowQueryPanel(false);
|
| 341 |
+
}}
|
| 342 |
+
>
|
| 343 |
+
👤 Register User
|
| 344 |
+
</button>
|
| 345 |
+
)}
|
| 346 |
+
|
| 347 |
<button
|
| 348 |
className="logout-btn"
|
| 349 |
+
onClick={() => {
|
| 350 |
+
setToken(null);
|
| 351 |
+
setMe(null);
|
| 352 |
+
setShowUpload(false);
|
| 353 |
+
setShowQueryPanel(false);
|
| 354 |
+
setShowRegisterPanel(false);
|
| 355 |
+
setRegisterStatus(null);
|
| 356 |
+
}}
|
| 357 |
>
|
| 358 |
Logout
|
| 359 |
</button>
|
|
|
|
| 456 |
)}
|
| 457 |
</aside>
|
| 458 |
)}
|
| 459 |
+
|
| 460 |
+
{/* Right Sidebar - Admin Register Panel */}
|
| 461 |
+
{showRegisterPanel && me && me.is_admin && (
|
| 462 |
+
<aside className="sidebar right-sidebar">
|
| 463 |
+
<h3>👤 Register User</h3>
|
| 464 |
+
<p style={{ fontSize: "0.9em", color: "#666" }}>
|
| 465 |
+
Create a new user account (admin only)
|
| 466 |
+
</p>
|
| 467 |
+
|
| 468 |
+
<form onSubmit={handleRegisterUser}>
|
| 469 |
+
<input
|
| 470 |
+
type="text"
|
| 471 |
+
placeholder="Username"
|
| 472 |
+
value={registerForm.username}
|
| 473 |
+
onChange={(e) => setRegisterForm({ ...registerForm, username: e.target.value })}
|
| 474 |
+
style={{ width: "100%", marginTop: "10px" }}
|
| 475 |
+
/>
|
| 476 |
+
<input
|
| 477 |
+
type="password"
|
| 478 |
+
placeholder="Password"
|
| 479 |
+
value={registerForm.password}
|
| 480 |
+
onChange={(e) => setRegisterForm({ ...registerForm, password: e.target.value })}
|
| 481 |
+
style={{ width: "100%", marginTop: "10px" }}
|
| 482 |
+
/>
|
| 483 |
+
|
| 484 |
+
<label style={{ display: "flex", alignItems: "center", gap: "8px", marginTop: "10px" }}>
|
| 485 |
+
<input
|
| 486 |
+
type="checkbox"
|
| 487 |
+
checked={registerForm.overwrite}
|
| 488 |
+
onChange={(e) => setRegisterForm({ ...registerForm, overwrite: e.target.checked })}
|
| 489 |
+
/>
|
| 490 |
+
Overwrite if user exists
|
| 491 |
+
</label>
|
| 492 |
+
|
| 493 |
+
<button type="submit" style={{ marginTop: "10px", width: "100%" }}>
|
| 494 |
+
Create User
|
| 495 |
+
</button>
|
| 496 |
+
</form>
|
| 497 |
+
|
| 498 |
+
{registerStatus && (
|
| 499 |
+
<div className="status-message" style={{ marginTop: "10px" }}>
|
| 500 |
+
{registerStatus}
|
| 501 |
+
</div>
|
| 502 |
+
)}
|
| 503 |
+
</aside>
|
| 504 |
+
)}
|
| 505 |
</div>
|
| 506 |
</div>
|
| 507 |
);
|
src/api.js
CHANGED
|
@@ -159,3 +159,63 @@ export async function queryProlog(query, token, maxSolutions = 10) {
|
|
| 159 |
return json;
|
| 160 |
}
|
| 161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
return json;
|
| 160 |
}
|
| 161 |
|
| 162 |
+
|
| 163 |
+
export async function getMe(token) {
|
| 164 |
+
const res = await fetch(`${API_BASE}/me`, {
|
| 165 |
+
method: "GET",
|
| 166 |
+
headers: {
|
| 167 |
+
Authorization: `Bearer ${token}`,
|
| 168 |
+
},
|
| 169 |
+
});
|
| 170 |
+
|
| 171 |
+
const text = await res.text().catch(() => "");
|
| 172 |
+
let json;
|
| 173 |
+
try {
|
| 174 |
+
json = text ? JSON.parse(text) : {};
|
| 175 |
+
} catch {
|
| 176 |
+
json = {};
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
if (!res.ok) {
|
| 180 |
+
const msg = (json && json.error) || text || `HTTP ${res.status}`;
|
| 181 |
+
throw new Error(msg);
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
if (!json.ok) {
|
| 185 |
+
throw new Error(json.error || "Failed to fetch user info");
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
return json; // { ok, username, is_admin }
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
export async function registerUser(username, password, token, overwrite = false) {
|
| 193 |
+
const res = await fetch(`${API_BASE}/register`, {
|
| 194 |
+
method: "POST",
|
| 195 |
+
headers: {
|
| 196 |
+
"Content-Type": "application/json",
|
| 197 |
+
Authorization: `Bearer ${token}`,
|
| 198 |
+
},
|
| 199 |
+
body: JSON.stringify({ username, password, overwrite }),
|
| 200 |
+
});
|
| 201 |
+
|
| 202 |
+
const text = await res.text().catch(() => "");
|
| 203 |
+
let json;
|
| 204 |
+
try {
|
| 205 |
+
json = text ? JSON.parse(text) : {};
|
| 206 |
+
} catch {
|
| 207 |
+
json = {};
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
if (!res.ok) {
|
| 211 |
+
const msg = (json && json.error) || text || `HTTP ${res.status}`;
|
| 212 |
+
throw new Error(msg);
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
if (!json.ok) {
|
| 216 |
+
throw new Error(json.error || "Register failed");
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
return json; // { ok, username }
|
| 220 |
+
}
|
| 221 |
+
|