Two gaps found while wiring auth into the data routes: 1. mcp_server/auth.py: HTTPBearer() defaults to auto_error=True, which rejects any request with no Authorization header before get_current_user's own "auth not configured -> allow as guest" check ever runs. That meant turning on Depends(get_current_user) anywhere 401'd every request even with auth fully disabled. Switched to auto_error=False and handle the missing-credentials case explicitly inside get_current_user instead. 2. Frontend never actually sent the stored token on data requests. auth.tsx only attached it to its own /api/auth/me check; api.ts's fetch() calls (chat, stats, documents, upload, delete) and every axios call elsewhere in the app (projects, kanban, hooks) had no Authorization header at all. Added an authHeaders() helper in api.ts, and a single axios request interceptor in auth.tsx that attaches the token app-wide since every component imports the same default axios instance. Known gap not addressed here: mcp_server/claude_code_mcp.py (the code-forge stdio MCP server) calls the REST API with no token at all. Harmless while auth is disabled (current state), but it will start getting 401'd the moment CLAUDE_OS_EMAIL/PASSWORD are actually set - needs a service-token bypass if that's ever turned on.
95 lines
2.5 KiB
TypeScript
95 lines
2.5 KiB
TypeScript
const API_BASE = import.meta.env.VITE_API_URL || 'http://localhost:8051';
|
|
|
|
function authHeaders(): Record<string, string> {
|
|
const token = localStorage.getItem('auth_token');
|
|
return token ? { Authorization: `Bearer ${token}` } : {};
|
|
}
|
|
|
|
export interface ChatResponse {
|
|
response: string;
|
|
sources?: Array<{
|
|
filename: string;
|
|
content: string;
|
|
score: number;
|
|
}>;
|
|
thinking_time?: number;
|
|
}
|
|
|
|
export interface KBStats {
|
|
document_count: number;
|
|
chunk_count: number;
|
|
last_updated?: string;
|
|
total_size?: number;
|
|
}
|
|
|
|
export interface Document {
|
|
filename: string;
|
|
chunk_count: number;
|
|
created_at?: string;
|
|
}
|
|
|
|
export async function chat(
|
|
kbName: string,
|
|
query: string,
|
|
options?: { useHybrid?: boolean; useRerank?: boolean; useAgentic?: boolean }
|
|
): Promise<ChatResponse> {
|
|
const res = await fetch(`${API_BASE}/api/kb/${kbName}/chat`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json', ...authHeaders() },
|
|
body: JSON.stringify({
|
|
query,
|
|
use_hybrid: options?.useHybrid ?? false,
|
|
use_rerank: options?.useRerank ?? false,
|
|
use_agentic: options?.useAgentic ?? false,
|
|
}),
|
|
});
|
|
|
|
if (!res.ok) {
|
|
throw new Error(`Chat failed: ${res.statusText}`);
|
|
}
|
|
|
|
return res.json();
|
|
}
|
|
|
|
export async function getKBStats(kbName: string): Promise<KBStats> {
|
|
const res = await fetch(`${API_BASE}/api/kb/${kbName}/stats`, { headers: authHeaders() });
|
|
if (!res.ok) {
|
|
throw new Error(`Failed to get KB stats: ${res.statusText}`);
|
|
}
|
|
return res.json();
|
|
}
|
|
|
|
export async function listDocuments(kbName: string): Promise<Document[]> {
|
|
const res = await fetch(`${API_BASE}/api/kb/${kbName}/documents`, { headers: authHeaders() });
|
|
if (!res.ok) {
|
|
throw new Error(`Failed to list documents: ${res.statusText}`);
|
|
}
|
|
const data = await res.json();
|
|
return data.documents || [];
|
|
}
|
|
|
|
export async function uploadDocument(kbName: string, file: File): Promise<void> {
|
|
const formData = new FormData();
|
|
formData.append('file', file);
|
|
|
|
const res = await fetch(`${API_BASE}/api/kb/${kbName}/upload`, {
|
|
method: 'POST',
|
|
headers: authHeaders(),
|
|
body: formData,
|
|
});
|
|
|
|
if (!res.ok) {
|
|
throw new Error(`Failed to upload document: ${res.statusText}`);
|
|
}
|
|
}
|
|
|
|
export async function deleteDocument(kbName: string, filename: string): Promise<void> {
|
|
const res = await fetch(`${API_BASE}/api/kb/${kbName}/documents/${encodeURIComponent(filename)}`, {
|
|
method: 'DELETE',
|
|
headers: authHeaders(),
|
|
});
|
|
|
|
if (!res.ok) {
|
|
throw new Error(`Failed to delete document: ${res.statusText}`);
|
|
}
|
|
}
|