summaryrefslogtreecommitdiff
path: root/frontend/src/store/flowStore.ts
diff options
context:
space:
mode:
authorblackhao <13851610112@163.com>2025-12-10 20:12:21 -0600
committerblackhao <13851610112@163.com>2025-12-10 20:12:21 -0600
commit9646da833bc3d94564c10649b62a378d0190471e (patch)
treed0c9c0584b8c4f167c281f5970f713b239a1d7c5 /frontend/src/store/flowStore.ts
parent9ba956c7aa601f0e6cd0fe2ede907cbc558fa1b8 (diff)
user data
Diffstat (limited to 'frontend/src/store/flowStore.ts')
-rw-r--r--frontend/src/store/flowStore.ts42
1 files changed, 33 insertions, 9 deletions
diff --git a/frontend/src/store/flowStore.ts b/frontend/src/store/flowStore.ts
index 498937e..de23c95 100644
--- a/frontend/src/store/flowStore.ts
+++ b/frontend/src/store/flowStore.ts
@@ -250,11 +250,35 @@ const getStableColor = (str: string) => {
return `hsl(${hue}, 70%, 60%)`;
};
+import { useAuthStore } from './authStore';
+
const API_BASE = import.meta.env.VITE_BACKEND_URL || 'http://localhost:8000';
-const DEFAULT_USER = 'test';
+const DEFAULT_USER = 'test'; // Fallback for unauthenticated requests
+
+// Get current username directly from authStore
+const getCurrentUser = () => {
+ const authState = useAuthStore.getState();
+ return authState.user?.username || DEFAULT_USER;
+};
+
+// Get auth headers directly from authStore
+const getAuthHeaders = (): Record<string, string> => {
+ const authState = useAuthStore.getState();
+ if (authState.token) {
+ return { Authorization: `Bearer ${authState.token}` };
+ }
+ return {};
+};
const jsonFetch = async <T>(url: string, options?: RequestInit): Promise<T> => {
- const res = await fetch(url, options);
+ const authHeaders = getAuthHeaders();
+ const res = await fetch(url, {
+ ...options,
+ headers: {
+ ...options?.headers,
+ ...authHeaders,
+ },
+ });
if (!res.ok) {
const detail = await res.text();
throw new Error(detail || `Request failed: ${res.status}`);
@@ -1474,7 +1498,7 @@ const useFlowStore = create<FlowState>((set, get) => {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
- user: DEFAULT_USER,
+ user: getCurrentUser(),
path,
content: payload,
}),
@@ -1485,14 +1509,14 @@ const useFlowStore = create<FlowState>((set, get) => {
readBlueprintFile: async (path: string): Promise<BlueprintDocument> => {
const res = await jsonFetch<{ content: BlueprintDocument }>(
- `${API_BASE}/api/projects/file?user=${encodeURIComponent(DEFAULT_USER)}&path=${encodeURIComponent(path)}`
+ `${API_BASE}/api/projects/file?user=${encodeURIComponent(getCurrentUser())}&path=${encodeURIComponent(path)}`
);
return validateBlueprint(res.content);
},
refreshProjectTree: async () => {
const tree = await jsonFetch<FSItem[]>(
- `${API_BASE}/api/projects/tree?user=${encodeURIComponent(DEFAULT_USER)}`
+ `${API_BASE}/api/projects/tree?user=${encodeURIComponent(getCurrentUser())}`
);
set({ projectTree: tree });
return tree;
@@ -1557,7 +1581,7 @@ const useFlowStore = create<FlowState>((set, get) => {
loadArchivedNodes: async () => {
const res = await jsonFetch<{ archived: ArchivedNode[] }>(
- `${API_BASE}/api/projects/archived?user=${encodeURIComponent(DEFAULT_USER)}`
+ `${API_BASE}/api/projects/archived?user=${encodeURIComponent(getCurrentUser())}`
);
set({ archivedNodes: res.archived || [] });
},
@@ -1574,7 +1598,7 @@ const useFlowStore = create<FlowState>((set, get) => {
// Files management
refreshFiles: async () => {
const res = await jsonFetch<{ files: FileMeta[] }>(
- `${API_BASE}/api/files?user=${encodeURIComponent(DEFAULT_USER)}`
+ `${API_BASE}/api/files?user=${encodeURIComponent(getCurrentUser())}`
);
set({ files: res.files || [] });
},
@@ -1592,7 +1616,7 @@ const useFlowStore = create<FlowState>((set, get) => {
form.append('purpose', purpose);
}
try {
- const res = await fetch(`${API_BASE}/api/files/upload?user=${encodeURIComponent(DEFAULT_USER)}`, {
+ const res = await fetch(`${API_BASE}/api/files/upload?user=${encodeURIComponent(getCurrentUser())}`, {
method: 'POST',
body: form,
});
@@ -1611,7 +1635,7 @@ const useFlowStore = create<FlowState>((set, get) => {
},
deleteFile: async (fileId: string) => {
- const res = await fetch(`${API_BASE}/api/files/delete?user=${encodeURIComponent(DEFAULT_USER)}&file_id=${encodeURIComponent(fileId)}`, {
+ const res = await fetch(`${API_BASE}/api/files/delete?user=${encodeURIComponent(getCurrentUser())}&file_id=${encodeURIComponent(fileId)}`, {
method: 'POST',
});
if (!res.ok) {