Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): honor auth-gate 401 redirect in streaming fetch

Adam Eivy00ca2be34e87
diff --git a/client/src/services/apiCore.js b/client/src/services/apiCore.js
index 6e46575e41..0f7076b2f9 100644
--- a/client/src/services/apiCore.js
+++ b/client/src/services/apiCore.js
@@ -5,6 +5,20 @@ export const API_BASE = '/api'; // exported for sub-modules that use fetch() dir
// Stable ID for the PortOS baseline app (mirrors server PORTOS_APP_ID)
export const PORTOS_APP_ID = 'portos-default';
+// Bounce to /login when the auth gate (server: lib/authGate.js) rejects a
+// request with 401 + AUTH_REQUIRED. Shared by request() AND the streaming-fetch
+// helpers (apiLocalLlm.streamLocalLlmTest, etc.) that bypass request() to read a
+// stream body but must still honor session expiry — otherwise a session that
+// lapses mid-feature just errors in place instead of re-authenticating.
+export function maybeRedirectToLogin(response, error) {
+ if (response.status === 401 && error?.code === 'AUTH_REQUIRED' && typeof window !== 'undefined') {
+ if (!window.location.pathname.startsWith('/login')) {
+ const next = encodeURIComponent(window.location.pathname + window.location.search);
+ window.location.replace(`/login?next=${next}`);
+ }
+ }
+}
+
export async function request(endpoint, options = {}) {
const { silent, responseType, ...fetchOptions } = options;
const url = `${API_BASE}${endpoint}`;
@@ -34,13 +48,7 @@ export async function request(endpoint, options = {}) {
// Auth gate (server: lib/authGate.js) returns 401 with code AUTH_REQUIRED
// for any /api request without a valid session. Bounce to /login so the
// user can re-authenticate; skip if we're already there.
- if (response.status === 401 && error?.code === 'AUTH_REQUIRED' && typeof window !== 'undefined') {
- const here = window.location.pathname + window.location.search;
- if (!window.location.pathname.startsWith('/login')) {
- const next = encodeURIComponent(here);
- window.location.replace(`/login?next=${next}`);
- }
- }
+ maybeRedirectToLogin(response, error);
if (!silent) {
// Platform unavailability is a warning, not an error
if (error.code === 'PLATFORM_UNAVAILABLE') {
diff --git a/client/src/services/apiLocalLlm.js b/client/src/services/apiLocalLlm.js
index c1abd48a43..0214bdfa5c 100644
--- a/client/src/services/apiLocalLlm.js
+++ b/client/src/services/apiLocalLlm.js
@@ -1,4 +1,4 @@
-import { request, API_BASE } from './apiCore.js';
+import { request, API_BASE, maybeRedirectToLogin } from './apiCore.js';
// Local LLM backends (Ollama / LM Studio) — status, model management, migrate.
// Installed models per backend come back inside getLocalLlmStatus().
@@ -82,6 +82,9 @@ export async function streamLocalLlmTest(payload, { signal, onToken } = {}) {
});
if (!response.ok || !response.body?.getReader) {
const err = await response.json().catch(() => ({ error: `HTTP ${response.status}` }));
+ // Honor session expiry the same way request() does — a streaming run that
+ // 401s should bounce to /login, not just toast and strand the user here.
+ maybeRedirectToLogin(response, err);
throw new Error(err.error || `HTTP ${response.status}`);
}