From 7a3945e2f1bb2c55af41c0088fe52634b6815497 Mon Sep 17 00:00:00 2001 From: Jannis Braun <151788261+TheZwiss@users.noreply.github.com> Date: Thu, 2 Apr 2026 08:44:05 +0200 Subject: [PATCH] =?UTF-8?q?fix(web):=20disconnect=20keeps=20token=20alive?= =?UTF-8?q?=20=E2=80=94=20reconnect=20without=20re-auth?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/web/src/stores/instanceStore.ts | 25 ++++++++++++------------ 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/packages/web/src/stores/instanceStore.ts b/packages/web/src/stores/instanceStore.ts index 0b54645b..2117e50b 100644 --- a/packages/web/src/stores/instanceStore.ts +++ b/packages/web/src/stores/instanceStore.ts @@ -425,10 +425,10 @@ export const useInstanceStore = create((set, get) => ({ }, disconnectInstance: (origin: string) => { - // Tear down WebSocket connection + // Tear down WebSocket connection (stops auto-reconnect) disconnectWs(origin); - // Update registry entry to disconnected (preserve the entry) + // Update registry entry to disconnected const registry = upsertRegistryEntry(get().registry, origin, { origin, status: 'disconnected', @@ -437,17 +437,16 @@ export const useInstanceStore = create((set, get) => ({ }); const registryUpdatedAt = Date.now(); - // Save the token BEFORE filtering it out of instances — this preserves - // the cached token in localStorage so reconnectInstance can restore it later. - const currentInstances = get().instances; - const userId = useAuthStore.getState().user?.id; - if (userId) saveCachedTokens(currentInstances, userId); - - set((state) => ({ - instances: state.instances.filter(i => i.origin !== origin), - registry, - registryUpdatedAt, - })); + // Keep the instance in the array with status 'disconnected' — preserves + // the token and API client so reconnect is instant (no re-auth needed). + set((state) => { + const updated = state.instances.map(i => + i.origin === origin ? { ...i, status: 'disconnected' as const, error: undefined } : i + ); + const userId = useAuthStore.getState().user?.id; + if (userId) saveCachedTokens(updated, userId); + return { instances: updated, registry, registryUpdatedAt }; + }); // Remove spaces from this instance from the space store useSpaceStore.getState().removeInstanceSpaces(origin);