fix(app): a re-read of the same account keeps its workspace on screen

USER_UPDATED (or a retry) set the workspace back to loading, which unmounted
the client routes and lost an open form's typed values. A ready workspace now
stays until the answer; found by the V1-1.5 security audit.
This commit is contained in:
bchanot committed 2026-10-26 02:08:25 +01:00
1 parent ae0bd3f8d8
commit e1c1980949
2 files changed
+22 -3

No files matched your search

+6 -3
View File
@@ -116,10 +116,13 @@ async function loadWorkspace(
user: User,
profile: Profile | null,
set: Set,
get: Get,
) {
if (!user.email_confirmed_at) return set({ workspace: { status: 'unconfirmed' } })
if (profile && !profile.active) return set({ workspace: { status: 'inactive' } })
set({ workspace: { status: 'loading' } })
// A re-read (USER_UPDATED, retry) keeps a ready workspace on screen until the
// answer, so the routes under it, an open form included, stay mounted.
if (get().workspace.status !== 'ready') set({ workspace: { status: 'loading' } })
try {
const id = await fetchPersonalWorkspaceId(client, user.id)
set({ workspace: id ? { status: 'ready', id } : { status: 'missing' } })
@@ -136,7 +139,7 @@ async function loadAccount(client: FaunosteoClient, user: User, set: Set, get: G
])
setOwn({ profile, roles })
await Promise.all([
loadWorkspace(client, user, profile, setOwn),
loadWorkspace(client, user, profile, setOwn, get),
syncAvatar(client, user, setOwn, get).catch(() => setOwn({ avatar: null })),
])
}
@@ -242,7 +245,7 @@ export const useAuthStore = create<AuthState>((set, get) => ({
refreshWorkspace: async () => {
const user = requireUser(get().user)
await loadWorkspace(requireClient(), user, get().profile, setForUser(user, set, get))
await loadWorkspace(requireClient(), user, get().profile, setForUser(user, set, get), get)
},
refreshAvatar: async () => {
@@ -298,6 +298,22 @@ describe('authStore', () => {
})
})
it('keeps a ready workspace on screen while the same account is read again', async () => {
fake.tables.workspaces = [{ id: 'ws-1', personal_user_id: 'user-1' }]
stop = useAuthStore.getState().start()
fake.emit('SIGNED_IN', fakeSession())
await flush()
await flush()
const seen: string[] = []
const unsubscribe = useAuthStore.subscribe((s) => seen.push(s.workspace.status))
fake.emit('USER_UPDATED', fakeSession())
await flush()
await flush()
unsubscribe()
expect(seen).not.toContain('loading')
expect(useAuthStore.getState().workspace).toEqual({ status: 'ready', id: 'ws-1' })
})
it('forgets the workspace on sign-out', async () => {
fake.tables.workspaces = [{ id: 'ws-1', personal_user_id: 'user-1' }]
stop = useAuthStore.getState().start()