Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<link href="./src/index.css" rel="stylesheet" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>NeuraMemory AI</title>
</head>
Expand Down
8 changes: 7 additions & 1 deletion client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ function ProtectedRoute({ children }: { children: React.ReactNode }) {
.catch(() => setStatus('unauth'));
}, []);

if (status === 'loading') return null;
if (status === 'loading') {
return (
<div className="flex items-center justify-center min-h-screen bg-neutral-950">
<div className="w-8 h-8 border-4 border-blue-500 border-t-transparent rounded-full animate-spin" />
</div>
);
}
if (status === 'unauth') return <Navigate to="/login" replace />;
return <>{children}</>;
}
Expand Down
17 changes: 17 additions & 0 deletions client/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,20 @@ export const api = axios.create({
baseURL: import.meta.env.VITE_API_URL || 'http://localhost:3000',
withCredentials: true,
});

// Automatically attach the CSRF token from cookie to state-changing requests.
// The server sets `csrf_token` cookie on first load (readable by JS since httpOnly=false).
api.interceptors.request.use((config) => {
const method = config.method?.toUpperCase();
const isStateChanging = ['POST', 'PUT', 'DELETE', 'PATCH'].includes(method ?? '');
if (isStateChanging) {
const csrfToken = document.cookie
.split('; ')
.find((row) => row.startsWith('csrf_token='))
?.split('=')?.[1];
if (csrfToken) {
config.headers['x-csrf-token'] = csrfToken;
}
}
return config;
});
2 changes: 1 addition & 1 deletion server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ app.use(
},
credentials: true,
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'x-api-key', 'mcp-session-id'],
allowedHeaders: ['Content-Type', 'Authorization', 'x-api-key', 'mcp-session-id', 'x-csrf-token'],
}),
);

Expand Down
Loading