From f14b0a8859f518e271b523feb7234092c635f0f9 Mon Sep 17 00:00:00 2001 From: ShihChi Huang Date: Fri, 18 Sep 2026 12:25:15 -0700 Subject: [PATCH 1/2] fix: gate cloud bootstrap on authentication --- src/stores/bootstrapStore.test.ts | 33 +++++++++++++++---------------- src/stores/bootstrapStore.ts | 17 ++++++++++------ 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/stores/bootstrapStore.test.ts b/src/stores/bootstrapStore.test.ts index 09fb61a6f15..ff05d7d460b 100644 --- a/src/stores/bootstrapStore.test.ts +++ b/src/stores/bootstrapStore.test.ts @@ -1,6 +1,7 @@ import { useAuthStore } from '@/stores/authStore' import { useUserStore } from '@/stores/userStore' import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore' +import { fromPartial } from '@total-typescript/shoehorn' import type { AxiosResponse } from 'axios' import { AxiosError } from 'axios' import { beforeEach, describe, expect, it, vi } from 'vitest' @@ -41,7 +42,7 @@ describe('bootstrapStore', () => { vi.spyOn(i18nModule, 'mergeCustomNodesI18n').mockImplementation(() => {}) useSettingStore().isReady = false useAuthStore().isInitialized = false - Object.assign(useAuthStore(), { isAuthenticated: false }) + Object.assign(useAuthStore(), { currentUser: null }) Object.assign(useUserStore(), { needsLogin: false }) mockDistributionTypes.isCloud = false vi.mocked(useSettingStore().load).mockImplementation(() => { @@ -129,32 +130,33 @@ describe('bootstrapStore', () => { mockReportError.mockReset() }) - it('waits for Firebase init before loading stores, then proceeds regardless of auth state', async () => { + it('waits for Firebase init before loading stores, then resumes after sign-in', async () => { const store = useBootstrapStore() const settingStore = useSettingStore() const bootstrapPromise = store.startStoreBootstrap() - expect(store.isI18nReady).toBe(false) expect(settingStore.isReady).toBe(false) - // Firebase resolves with no user (signed-out) — bootstrap must unblock. - // Previously it also waited for isAuthenticated, which made every - // signed-out load wait 35s and fire a false Sentry timeout. useAuthStore().isInitialized = true - await bootstrapPromise await vi.waitFor(() => { expect(store.isI18nReady).toBe(true) - expect(settingStore.isReady).toBe(true) }) + expect(settingStore.isReady).toBe(false) + expect(useWorkflowStore().loadWorkflows).not.toHaveBeenCalled() + + useAuthStore().currentUser = fromPartial({ uid: 'user-123' }) + await bootstrapPromise + + expect(settingStore.isReady).toBe(true) }) - it('retries once and proceeds if Firebase init resolves during the backoff', async () => { + it('retries once and keeps authenticated stores gated if Firebase resolves signed out', async () => { vi.useFakeTimers() try { const store = useBootstrapStore() const settingStore = useSettingStore() - const bootstrapPromise = store.startStoreBootstrap() + void store.startStoreBootstrap() // First wait times out with Firebase still not initialized. await vi.advanceTimersByTimeAsync(16_001) @@ -163,16 +165,15 @@ describe('bootstrapStore', () => { // Firebase resolves during the retry backoff. useAuthStore().isInitialized = true await vi.advanceTimersByTimeAsync(3_001) - await bootstrapPromise - expect(settingStore.isReady).toBe(true) + expect(settingStore.isReady).toBe(false) expect(mockReportError).not.toHaveBeenCalled() } finally { vi.useRealTimers() } }) - it('gives up after a second timeout, reports it, and continues bootstrap unauthenticated', async () => { + it('gives up after a second timeout, reports it, and keeps authenticated stores gated', async () => { vi.useFakeTimers() const consoleError = vi .spyOn(console, 'error') @@ -180,19 +181,17 @@ describe('bootstrapStore', () => { try { const store = useBootstrapStore() const settingStore = useSettingStore() - const bootstrapPromise = store.startStoreBootstrap() + void store.startStoreBootstrap() // Firebase never resolves through the initial wait, the backoff, or the retry. await vi.advanceTimersByTimeAsync(16_000 + 3_000 + 16_001) - await bootstrapPromise expect(mockReportError).toHaveBeenCalledOnce() expect(mockReportError).toHaveBeenCalledWith(expect.anything(), { errorType: 'bootstrap_auth_wait_timeout' }) expect(consoleError).not.toHaveBeenCalled() - // Bootstrap must not stay stuck: stores load even when Firebase never fires. - expect(settingStore.isReady).toBe(true) + expect(settingStore.isReady).toBe(false) } finally { vi.useRealTimers() } diff --git a/src/stores/bootstrapStore.ts b/src/stores/bootstrapStore.ts index 3d52bcd2be3..9f70a3bdd04 100644 --- a/src/stores/bootstrapStore.ts +++ b/src/stores/bootstrapStore.ts @@ -35,10 +35,9 @@ const AUTH_WAIT_RETRY_DELAY_MS = 3_000 * token or a broken auth response can never hang bootstrap forever. * * Only isInitialized is awaited — onAuthStateChanged fires with null for - * signed-out users, which sets isInitialized but not isAuthenticated. - * Awaiting isAuthenticated here would make every signed-out page load wait - * 35s and fire a false Sentry timeout. The router guard handles the - * login redirect for unauthenticated users separately. + * signed-out users, so a signed-out load resolves this wait immediately + * instead of burning the timeout and firing a false Sentry report. Waiting + * for an actual user is a separate, unbounded wait in the caller. * * Retries once after a short delay; if auth is still unresolved, reports it * to every observability sink and lets bootstrap continue rather than leaving @@ -103,8 +102,16 @@ export const useBootstrapStore = defineStore('bootstrap', () => { } async function startStoreBootstrap() { + void loadI18n() + if (isCloud) { await bootstrapTracer.settle('auth-gate/initialized', waitForCloudAuth) + + // Signed-out cloud pages (/cloud/login) must issue no authenticated + // request, so bootstrap parks here until the user signs in rather than + // loading stores that can only answer 401. + const { isAuthenticated } = storeToRefs(useAuthStore()) + await until(isAuthenticated).toBe(true) } const userStore = useUserStore() @@ -117,9 +124,7 @@ export const useBootstrapStore = defineStore('bootstrap', () => { until(needsLogin).toBe(false) ) - void loadI18n() const storeLoads = loadAuthenticatedStores() - void Promise.allSettled(storeLoads).then(() => { bootstrapTracer.milestone('stores-ready') }) From 03879e869c0c7911e9d28c8c03c916c252ee32af Mon Sep 17 00:00:00 2001 From: ShihChi Huang Date: Mon, 21 Sep 2026 10:41:32 -0700 Subject: [PATCH 2/2] test: assert translations load before Firebase initializes --- src/stores/bootstrapStore.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/stores/bootstrapStore.test.ts b/src/stores/bootstrapStore.test.ts index ff05d7d460b..997d5b69521 100644 --- a/src/stores/bootstrapStore.test.ts +++ b/src/stores/bootstrapStore.test.ts @@ -137,6 +137,11 @@ describe('bootstrapStore', () => { expect(settingStore.isReady).toBe(false) + await vi.waitFor(() => { + expect(store.isI18nReady).toBe(true) + }) + expect(useAuthStore().isInitialized).toBe(false) + useAuthStore().isInitialized = true await vi.waitFor(() => {