Skip to content

armgit / AiSOC

Public Imported from GitHub
0

Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.

fix(web): force SWR revalidation + demo auto-login so /cases shows live data

Beenu Arorae07dbc0b2966
diff --git a/apps/web/src/components/demo/DemoAutoLogin.tsx b/apps/web/src/components/demo/DemoAutoLogin.tsx
new file mode 100644
index 000000000..1b88c76cf
--- /dev/null
+++ b/apps/web/src/components/demo/DemoAutoLogin.tsx
@@ -0,0 +1,72 @@
+'use client';
+
+/**
+ * DemoAutoLogin — silent auto-login for the hosted demo at tryaisoc.com.
+ *
+ * On the public demo we never want a visitor to land on `/cases` and see mock
+ * rows because no JWT is in `localStorage`. This component runs once on mount
+ * inside `AppShell`, and when:
+ *
+ * 1. `isDemoMode()` is true (NEXT_PUBLIC_DEMO_MODE=true), AND
+ * 2. there's no existing access token,
+ *
+ * it logs in as the well-known demo user (`[email protected]` / `aisoc-demo` —
+ * the same credentials displayed on `/login`) and then triggers a global SWR
+ * revalidation so every `useSWR` hook on the page swaps its `fallbackData`
+ * mocks for the freshly-fetched live data.
+ *
+ * Renders nothing. Failures are swallowed silently — most demo endpoints
+ * (cases list, alerts list, dashboard widgets) work without a JWT thanks to
+ * the demo tenant default, so a failed auto-login still leaves the page
+ * readable.
+ *
+ * Self-hosted builds skip this entirely because `isDemoMode()` returns false.
+ */
+
+import { useEffect } from 'react';
+import { useSWRConfig } from 'swr';
+import { authApi } from '@/lib/api';
+import { isDemoMode } from '@/lib/demoMode';
+
+const DEMO_EMAIL =
+ process.env.NEXT_PUBLIC_DEMO_AUTOLOGIN_EMAIL?.trim() || '[email protected]';
+const DEMO_PASSWORD =
+ process.env.NEXT_PUBLIC_DEMO_AUTOLOGIN_PASSWORD?.trim() || 'aisoc-demo';
+
+export function DemoAutoLogin() {
+ const { mutate } = useSWRConfig();
+
+ useEffect(() => {
+ // Only run client-side. Demo flag is build-time inlined, but we double
+ // check at runtime so a test override via `__setDemoModeForTests` works.
+ if (!isDemoMode()) return;
+ if (typeof window === 'undefined') return;
+
+ // Already authenticated? Nothing to do — let the existing session ride.
+ if (authApi.isAuthenticated()) return;
+
+ let cancelled = false;
+ (async () => {
+ try {
+ await authApi.login(DEMO_EMAIL, DEMO_PASSWORD);
+ if (cancelled) return;
+ // Force every SWR key on the page to refetch with the new bearer
+ // token. Passing `() => true` matches all keys; `undefined` data
+ // tells SWR to drop its cache entry and rerun the fetcher.
+ await mutate(() => true, undefined, { revalidate: true });
+ } catch {
+ // Swallow — most read endpoints still work without auth on the
+ // demo tenant, so a failed auto-login degrades gracefully.
+ }
+ })();
+
+ return () => {
+ cancelled = true;
+ };
+ // mutate is stable across renders per SWR docs; keep deps minimal so the
+ // effect runs exactly once per mount.
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, []);
+
+ return null;
+}
diff --git a/apps/web/src/components/layout/AppShell.tsx b/apps/web/src/components/layout/AppShell.tsx
index cc4d75280..032d9484b 100644
--- a/apps/web/src/components/layout/AppShell.tsx
+++ b/apps/web/src/components/layout/AppShell.tsx
@@ -1,10 +1,12 @@
'use client';
+import { SWRConfig } from 'swr';
import { Sidebar } from './Sidebar';
import { TopBar } from './TopBar';
import { CommandPalette } from './CommandPalette';
import { CopilotDock } from '@/components/copilot/CopilotDock';
import { DemoBanner } from '@/components/demo/DemoBanner';
+import { DemoAutoLogin } from '@/components/demo/DemoAutoLogin';
import { ClientOnly } from '@/components/util/ClientOnly';
import { isDemoMode } from '@/lib/demoMode';
@@ -19,28 +21,45 @@ export function AppShell({ children }: AppShellProps) {
const topPadClass = demo ? 'pt-[100px]' : 'pt-16';
return (
- <div className="min-h-screen bg-[#0a0d14]">
- <DemoBanner />
- <Sidebar />
- <div className="md:ml-60">
- <TopBar demoOffset={demo} />
- <main className={`${topPadClass} min-h-screen`}>
- <div className="p-6">{children}</div>
- </main>
- </div>
+ // SWR v2 disables `revalidateOnMount` by default whenever `fallbackData`
+ // is provided. Most views in the console pass `fallbackData: MOCK_*` so
+ // that the page renders instantly with placeholder data, but the side
+ // effect was that the real fetcher never ran — every page silently froze
+ // on its mocks (e.g. /cases showed seeded `case-1000` rows even though
+ // /api/v1/cases worked). Forcing both flags here makes mocks act as a
+ // first-paint placeholder and the live fetch always run on mount.
+ <SWRConfig value={{ revalidateOnMount: true, revalidateIfStale: true }}>
{/*
- Floating Copilot launcher and global command palette both rely on
- Framer Motion, which serializes inline `transform` styles differently
- on server vs client and triggers a hydration mismatch (React #418).
- Wrapping them in ClientOnly defers their entire render to after mount
- so SSR ships nothing for these subtrees.
+ Silently grants demo visitors a real JWT so SWR fetchers send a
+ bearer and views like /cases swap their `fallbackData` mocks for
+ live rows. Must sit *inside* SWRConfig so its post-login
+ `mutate(() => true)` reaches every key in the same SWR cache.
+ No-ops outside demo mode.
*/}
- <ClientOnly>
- <CopilotDock />
- </ClientOnly>
- <ClientOnly>
- <CommandPalette />
- </ClientOnly>
- </div>
+ <DemoAutoLogin />
+ <div className="min-h-screen bg-[#0a0d14]">
+ <DemoBanner />
+ <Sidebar />
+ <div className="md:ml-60">
+ <TopBar demoOffset={demo} />
+ <main className={`${topPadClass} min-h-screen`}>
+ <div className="p-6">{children}</div>
+ </main>
+ </div>
+ {/*
+ Floating Copilot launcher and global command palette both rely on
+ Framer Motion, which serializes inline `transform` styles differently
+ on server vs client and triggers a hydration mismatch (React #418).
+ Wrapping them in ClientOnly defers their entire render to after mount
+ so SSR ships nothing for these subtrees.
+ */}
+ <ClientOnly>
+ <CopilotDock />
+ </ClientOnly>
+ <ClientOnly>
+ <CommandPalette />
+ </ClientOnly>
+ </div>
+ </SWRConfig>
);
}