Skip to content

Open source personal finance application

fix: preserve offline queue on sign-out, show loading screen for first login

Ilya Komichevd91145a5790f
diff --git a/app/components/app/useInitApp.ts b/app/components/app/useInitApp.ts
index 146e29e5..70bebba0 100644
--- a/app/components/app/useInitApp.ts
+++ b/app/components/app/useInitApp.ts
@@ -77,8 +77,11 @@ export function useInitApp() {
// Clear cached data from localforage to prevent data leaks on shared devices.
// The offline queue is intentionally preserved — it has its own userId ownership check.
- for (const key of Object.values(STORAGE_KEYS))
- localforage.removeItem(key)
+ const offlineKeys = new Set([STORAGE_KEYS.offlineQueue, STORAGE_KEYS.offlineQueueUserId])
+ for (const key of Object.values(STORAGE_KEYS)) {
+ if (!offlineKeys.has(key))
+ localforage.removeItem(key)
+ }
}
async function loadDataFromDB() {
@@ -90,6 +93,11 @@ export function useInitApp() {
const { $waitForConvexAuth } = useNuxtApp()
await ($waitForConvexAuth as () => Promise<void>)()
+ // No authenticated user — skip data loading and replay to avoid
+ // overwriting cached stores with empty data or replaying without auth.
+ if (!userStore.uid)
+ return
+
// Set in-memory userId for queue ownership stamping during pushes
setOfflineQueueUserId(userStore.uid)
diff --git a/app/components/offline/helpers.ts b/app/components/offline/helpers.ts
index d3db3b22..84294f00 100644
--- a/app/components/offline/helpers.ts
+++ b/app/components/offline/helpers.ts
@@ -2,10 +2,11 @@ import localforage from 'localforage'
import type { EntityType, OfflineOp, OfflineOpInput } from '~/components/offline/types'
+import { STORAGE_KEYS } from '~/components/offline/storageKeys'
import { createLogger } from '~/utils/logger'
-const QUEUE_KEY = 'finapp.offline.queue'
-const QUEUE_USER_KEY = 'finapp.offline.queueUserId'
+const QUEUE_KEY = STORAGE_KEYS.offlineQueue
+const QUEUE_USER_KEY = STORAGE_KEYS.offlineQueueUserId
const logger = createLogger('offline')
diff --git a/app/components/offline/storageKeys.ts b/app/components/offline/storageKeys.ts
index 0c09abc1..86481986 100644
--- a/app/components/offline/storageKeys.ts
+++ b/app/components/offline/storageKeys.ts
@@ -1,6 +1,8 @@
export const STORAGE_KEYS = {
categories: 'finapp.categories',
currencies: 'finapp.currencies',
+ offlineQueue: 'finapp.offline.queue',
+ offlineQueueUserId: 'finapp.offline.queueUserId',
trns: 'finapp.trns',
trnsSyncMeta: 'finapp.trns.syncMeta',
user: 'finapp.user',
diff --git a/app/components/user/useUserStore.ts b/app/components/user/useUserStore.ts
index d1e937b1..b87d2ae7 100644
--- a/app/components/user/useUserStore.ts
+++ b/app/components/user/useUserStore.ts
@@ -198,7 +198,16 @@ export const useUserStore = defineStore('user', () => {
clearAuthCookie()
setSessionInitialized(false)
useCookie<boolean>('finapp.isOnboarded').value = false
- await localforage.clear()
+
+ // Clear cached data but preserve the offline queue — it has its own
+ // userId ownership check and will be replayed or cleared on next login.
+ const offlineKeys = new Set([STORAGE_KEYS.offlineQueue, STORAGE_KEYS.offlineQueueUserId])
+ await Promise.all(
+ Object.values(STORAGE_KEYS)
+ .filter(key => !offlineKeys.has(key))
+ .map(key => localforage.removeItem(key)),
+ )
+
await authClient.signOut()
}
catch (error) {
diff --git a/app/layouts/default.vue b/app/layouts/default.vue
index 8490e1e7..0546bc1d 100644
--- a/app/layouts/default.vue
+++ b/app/layouts/default.vue
@@ -19,7 +19,7 @@ const trnsStore = useTrnsStore()
const { isDemo } = useDemo()
const { isMenuOpen } = useMenuData()
const { isSearchOpen } = useSearch()
-const { loadDataFromCache, loadDataFromDB } = useInitApp()
+const { isDbLoading, loadDataFromCache, loadDataFromDB } = useInitApp()
const { width } = useWindowSize()
const isShowSidebar = useCookie('finapp.isShowSidebar', { default: () => true })
@@ -37,8 +37,15 @@ const { error, status } = useAsyncData(
}
else if (userStore.currentUser || hasAuthCookie()) {
await loadDataFromCache()
- if (navigator.onLine)
- loadDataFromDB()
+
+ if (navigator.onLine) {
+ // If cache had data, load from DB in background (user sees cached UI).
+ // If no cache (first login), await so the loading screen stays visible.
+ if (isOnboarded.value)
+ loadDataFromDB()
+ else
+ await loadDataFromDB()
+ }
}
},
)
@@ -72,7 +79,11 @@ defineShortcuts({
}"
style="margin-left: env(safe-area-inset-left)"
>
- <div v-if="status === 'error'" class="p-4">
+ <div v-if="status === 'pending' && !isOnboardedHint" class="flex-center h-dvh">
+ <UiLogo size="lg" />
+ </div>
+
+ <div v-else-if="status === 'error'" class="p-4">
<pre>{{ error }}</pre>
</div>