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

fix: suppress password risk warning when server is unreachable

Adam Eivy9da7a65d20af
diff --git a/client/src/components/PasswordRiskWarning.jsx b/client/src/components/PasswordRiskWarning.jsx
index 9cf0f816f3..dc0cae790c 100644
--- a/client/src/components/PasswordRiskWarning.jsx
+++ b/client/src/components/PasswordRiskWarning.jsx
@@ -1,7 +1,9 @@
import { useCallback, useEffect, useRef, useState } from 'react';
import { Link, useLocation } from 'react-router';
import { getAuthStatus, getPasswordRiskStatus } from '../services/apiAuth.js';
+import { isServerUnreachable } from '../services/apiCore.js';
import { safeReadStorage, safeWriteStorage, safeRemoveStorage } from '../lib/safeStorage.js';
+import socket from '../services/socket.js';
import Modal from './ui/Modal.jsx';
const ACKNOWLEDGEMENT_KEY = 'portos-password-risk-v1';
@@ -17,18 +19,30 @@ export default function PasswordRiskWarning() {
const generation = useRef(0);
const load = useCallback(() => {
const current = ++generation.current;
- const showCheckError = async () => {
+ const showCheckError = async (initialErr) => {
// Older servers and transient failures may make the risk endpoint unavailable.
// The login gate's public endpoint can still confirm password protection.
- const auth = await getAuthStatus({ silent: true }).catch(() => null);
- if (current !== generation.current) return;
- if (auth?.enabled === true) {
- setStatus({ enabled: true });
- setError('');
- return;
+ try {
+ const auth = await getAuthStatus({ silent: true });
+ if (current !== generation.current) return;
+ if (auth?.enabled === true) {
+ setStatus({ enabled: true });
+ setError('');
+ return;
+ }
+ setStatus(null);
+ setError('PortOS could not confirm password protection. Check Security settings or retry.');
+ } catch (authErr) {
+ if (current !== generation.current) return;
+ if (isServerUnreachable(authErr) || isServerUnreachable(initialErr)) {
+ // If the server is unreachable (restarting, updating, offline),
+ // do not clear known status or show a confusing warning about setting a password.
+ setError('');
+ return;
+ }
+ setStatus(null);
+ setError('PortOS could not confirm password protection. Check Security settings or retry.');
}
- setStatus(null);
- setError('PortOS could not confirm password protection. Check Security settings or retry.');
};
getPasswordRiskStatus({ silent: true }).then((value) => {
if (current !== generation.current) return;
@@ -40,20 +54,28 @@ export default function PasswordRiskWarning() {
setAccepted(false);
setStatus(value);
setError('');
- }).catch(() => {
+ }).catch((err) => {
if (current !== generation.current) return;
- return showCheckError();
+ if (isServerUnreachable(err)) {
+ setError('');
+ return;
+ }
+ return showCheckError(err);
});
}, []);
useEffect(() => {
load();
window.addEventListener('focus', load);
+ window.addEventListener('online', load);
window.addEventListener('portos:auth-changed', load);
+ socket.on('connect', load);
return () => {
generation.current += 1;
window.removeEventListener('focus', load);
+ window.removeEventListener('online', load);
window.removeEventListener('portos:auth-changed', load);
+ socket.off('connect', load);
};
}, [load, pathname]);
diff --git a/client/src/components/PasswordRiskWarning.test.jsx b/client/src/components/PasswordRiskWarning.test.jsx
index 6420cb8915..24cbb4ec73 100644
--- a/client/src/components/PasswordRiskWarning.test.jsx
+++ b/client/src/components/PasswordRiskWarning.test.jsx
@@ -3,8 +3,16 @@ import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/re
import { Link, MemoryRouter } from 'react-router';
import PasswordRiskWarning from './PasswordRiskWarning.jsx';
import { getAuthStatus, getPasswordRiskStatus } from '../services/apiAuth.js';
+import socket from '../services/socket.js';
vi.mock('../services/apiAuth.js', () => ({ getAuthStatus: vi.fn(), getPasswordRiskStatus: vi.fn() }));
+vi.mock('../services/socket.js', () => ({
+ default: { on: vi.fn(), off: vi.fn(), emit: vi.fn(), connected: true },
+}));
+const fireSocketEvent = (event) => {
+ for (const [name, handler] of socket.on.mock.calls) if (name === event) handler({});
+};
+
const renderWarning = () => render(<MemoryRouter><PasswordRiskWarning /><Link to="/">Home</Link><Link to="/apps">Apps</Link></MemoryRouter>);
beforeEach(() => {
vi.resetAllMocks();
@@ -110,4 +118,60 @@ describe('password risk warning', () => {
fireEvent.click(screen.getByRole('button', { name: 'Retry' }));
expect(await screen.findByRole('checkbox')).toBeInTheDocument();
});
+
+ it('does not show password warning when server is unreachable', async () => {
+ const unreachableErr = Object.assign(new Error('Server unreachable — check your connection and try again'), {
+ code: 'SERVER_UNREACHABLE',
+ });
+ getPasswordRiskStatus.mockRejectedValue(unreachableErr);
+ getAuthStatus.mockRejectedValue(unreachableErr);
+ renderWarning();
+ await waitFor(() => expect(getPasswordRiskStatus).toHaveBeenCalled());
+ expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
+ expect(screen.queryByRole('alert')).not.toBeInTheDocument();
+ });
+
+ it('preserves protected status and avoids warning during server restart, then recovers on reconnect', async () => {
+ // Initially protected
+ getPasswordRiskStatus.mockResolvedValue({ enabled: true, revision: 'pw-1' });
+ renderWarning();
+ await waitFor(() => expect(getPasswordRiskStatus).toHaveBeenCalled());
+ expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
+
+ // Server goes down (restart during update)
+ const unreachableErr = Object.assign(new Error('Server unreachable — check your connection and try again'), {
+ code: 'SERVER_UNREACHABLE',
+ });
+ getPasswordRiskStatus.mockRejectedValue(unreachableErr);
+ getAuthStatus.mockRejectedValue(unreachableErr);
+
+ fireEvent(window, new Event('focus'));
+ await waitFor(() => expect(getPasswordRiskStatus).toHaveBeenCalledTimes(2));
+ expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
+ expect(screen.queryByRole('alert')).not.toBeInTheDocument();
+
+ // Reconnects after restart still protected
+ getPasswordRiskStatus.mockResolvedValue({ enabled: true, revision: 'pw-1' });
+ fireSocketEvent('connect');
+ await waitFor(() => expect(getPasswordRiskStatus).toHaveBeenCalledTimes(3));
+ expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
+ });
+
+ it('shows password risk warning when server reconnects without a password', async () => {
+ // Initial unreachable state (e.g. fresh reload while server is updating)
+ const unreachableErr = Object.assign(new Error('Server unreachable — check your connection and try again'), {
+ code: 'SERVER_UNREACHABLE',
+ });
+ getPasswordRiskStatus.mockRejectedValue(unreachableErr);
+ getAuthStatus.mockRejectedValue(unreachableErr);
+ renderWarning();
+ await waitFor(() => expect(getPasswordRiskStatus).toHaveBeenCalled());
+ expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
+
+ // Server comes back online and reports no password set
+ getPasswordRiskStatus.mockResolvedValue({ enabled: false, revision: 'unprotected-rev' });
+ fireSocketEvent('connect');
+ expect(await screen.findByRole('dialog')).toBeInTheDocument();
+ expect(screen.getByRole('checkbox')).toBeInTheDocument();
+ });
});
diff --git a/client/src/services/apiCore.js b/client/src/services/apiCore.js
index ad332c48c9..f6f2981bf0 100644
--- a/client/src/services/apiCore.js
+++ b/client/src/services/apiCore.js
@@ -68,7 +68,9 @@ export async function request(endpoint, options = {}) {
if (!response) {
const msg = 'Server unreachable — check your connection and try again';
if (!silent) toast.error(msg);
- throw new Error(msg);
+ const err = new Error(msg);
+ err.code = 'SERVER_UNREACHABLE';
+ throw err;
}
if (!response.ok) {
@@ -151,3 +153,12 @@ export function uploadBody(data, filename) {
}
return JSON.stringify({ data, ...(filename ? { filename } : {}) });
}
+
+export function isServerUnreachable(err) {
+ if (typeof navigator !== 'undefined' && navigator.onLine === false) return true;
+ if (!err) return false;
+ if (err.code === 'SERVER_UNREACHABLE' || err.code === 'ECONNREFUSED' || err.code === 'ENOTFOUND') return true;
+ if (err.status === 502 || err.status === 503 || err.status === 504) return true;
+ const msg = typeof err === 'string' ? err : err.message || '';
+ return /server unreachable|failed to fetch|networkerror|load failed|econnrefused|net::err/i.test(msg);
+}
diff --git a/client/src/services/apiCore.test.js b/client/src/services/apiCore.test.js
index d494171ac7..56f8715fa6 100644
--- a/client/src/services/apiCore.test.js
+++ b/client/src/services/apiCore.test.js
@@ -13,7 +13,7 @@ vi.mock('../components/ui/Toast', () => {
});
import toast from '../components/ui/Toast';
-import { throwApiError, request, uploadBody } from './apiCore.js';
+import { throwApiError, request, uploadBody, isServerUnreachable } from './apiCore.js';
const makeResponse = ({ status = 400, ok = false, json = null } = {}) => ({
status,
@@ -104,6 +104,36 @@ describe('request() error path (now delegating to throwApiError)', () => {
expect(toast).not.toHaveBeenCalled();
expect(toast.error).not.toHaveBeenCalled();
});
+
+ it('rejects with SERVER_UNREACHABLE code when fetch fails to connect', async () => {
+ global.fetch.mockRejectedValue(new Error('Connection refused'));
+ await expect(request('/x', { silent: true })).rejects.toMatchObject({
+ message: expect.stringContaining('Server unreachable'),
+ code: 'SERVER_UNREACHABLE',
+ });
+ });
+});
+
+describe('isServerUnreachable', () => {
+ it('identifies server unreachable error codes and messages', () => {
+ expect(isServerUnreachable(new Error('Server unreachable — check your connection and try again'))).toBe(true);
+ expect(isServerUnreachable(Object.assign(new Error('down'), { code: 'SERVER_UNREACHABLE' }))).toBe(true);
+ expect(isServerUnreachable(Object.assign(new Error('refused'), { code: 'ECONNREFUSED' }))).toBe(true);
+ expect(isServerUnreachable(Object.assign(new Error('not found'), { code: 'ENOTFOUND' }))).toBe(true);
+ expect(isServerUnreachable(Object.assign(new Error('gateway timeout'), { status: 504 }))).toBe(true);
+ expect(isServerUnreachable(Object.assign(new Error('bad gateway'), { status: 502 }))).toBe(true);
+ expect(isServerUnreachable(Object.assign(new Error('service unavailable'), { status: 503 }))).toBe(true);
+ expect(isServerUnreachable(new TypeError('Failed to fetch'))).toBe(true);
+ expect(isServerUnreachable(new TypeError('NetworkError when attempting to fetch resource'))).toBe(true);
+ });
+
+ it('returns false for reachable server errors and empty values', () => {
+ expect(isServerUnreachable(null)).toBe(false);
+ expect(isServerUnreachable(undefined)).toBe(false);
+ expect(isServerUnreachable(new Error('Invalid password'))).toBe(false);
+ expect(isServerUnreachable(Object.assign(new Error('Not found'), { status: 404 }))).toBe(false);
+ expect(isServerUnreachable(Object.assign(new Error('Unauthorized'), { status: 401 }))).toBe(false);
+ });
});