fix(ui): hold the socket auth scope in state, require one session read

Review follow-ups. The previous sign-in scope now lives in state and is
adjusted during render, instead of a ref written while rendering. The
returning-visit browser check requires exactly one session read, so it
fails if the session is no longer verified.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DottaandClaude Opus 5.5 committed 2026-11-10 10:53:00 -06:00
1 parent b7c41490cc
commit 8e6e77dd4c
2 files changed
+10 -7

No files matched your search

@@ -25,6 +25,8 @@ test("a returning visit makes each shell read at most once and keeps one live so
await rowsAt();
await page.waitForTimeout(5_000);
const counts = shellReadCounts();
// The restored session is verified exactly once; that read is what used to reconnect the socket.
expect(counts["auth/get-session"], JSON.stringify(counts)).toBe(1);
expect(Object.entries(counts).filter(([, count]) => count > 1), JSON.stringify(counts)).toEqual([]);
expect(sockets, "The verified session reconnected the live socket").toHaveLength(1);
} finally { await context.close(); }
+8 -7
View File
@@ -2235,10 +2235,8 @@ type SocketAuthScope = { userId: string | null; sessionId: string | null; key: s
* reconnects.
*/
function nextSocketAuthScope(previous: SocketAuthScope | null, userId: string | null, sessionId: string | null): SocketAuthScope {
if (previous && userId !== null && previous.userId === userId) {
if (previous.sessionId === sessionId) return previous;
if (previous.sessionId === null) return { ...previous, sessionId };
}
if (previous && previous.userId === userId && previous.sessionId === sessionId) return previous;
if (previous && userId !== null && previous.userId === userId && previous.sessionId === null) return { ...previous, sessionId };
return { userId, sessionId, key: sessionId ?? userId ?? "signed_out" };
}
@@ -2269,9 +2267,12 @@ export function LiveUpdatesProvider({ children }: { children: ReactNode }) {
const { data: health } = useQuery({ queryKey: queryKeys.health, queryFn: healthApi.get });
const currentUserId = session?.user?.id ?? session?.session?.userId ??
(health?.deploymentMode === "local_trusted" ? "local-board" : null);
const socketAuth = useRef<SocketAuthScope | null>(null);
socketAuth.current = nextSocketAuthScope(socketAuth.current, currentUserId, session?.session?.id ?? null);
const socketAuthKey = socketAuth.current.key;
const sessionId = session?.session?.id ?? null;
const [socketAuth, setSocketAuth] = useState(() => nextSocketAuthScope(null, currentUserId, sessionId));
const nextSocketAuth = nextSocketAuthScope(socketAuth, currentUserId, sessionId);
// Adjust state during render: the scope depends on the previous scope.
if (nextSocketAuth !== socketAuth) setSocketAuth(nextSocketAuth);
const socketAuthKey = nextSocketAuth.key;
const [connectedScopeKey, setConnectedScopeKey] = useState<string | null>(null);
const liveCompanyId = resolveLiveCompanyId(selectedCompanyId, selectedCompany?.id ?? null);
const socketScopeKey = JSON.stringify([liveCompanyId, socketAuthKey]);