Skip to content

Commit

Permalink
fix: make sure the store is loaded when logging out
Browse files Browse the repository at this point in the history
Previously it was possible (and common in the case where the user hit a page to log out) for store to be undefined, because setupClient had not yet run. This actively loads the store before calling reset, which is much more reliable.
  • Loading branch information
travis committed Nov 29, 2023
1 parent d0cacd3 commit eb52faf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 42 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@ucanto/interface": "^9.0.0",
"@ucanto/principal": "^9.0.0",
"@ucanto/transport": "^9.0.0",
"@web3-storage/access": "^18.0.3",
"@web3-storage/access": "^18.0.5",
"@web3-storage/did-mailto": "^2.0.2",
"@web3-storage/w3up-client": "^11.2.0"
},
Expand Down
29 changes: 13 additions & 16 deletions packages/react/src/providers/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import type {
ContextActions,
ServiceConfig,
Space,
Account,
Store
Account
} from '@w3ui/core'

import React, { createContext, useState, useContext, useEffect, ReactNode } from 'react'
Expand Down Expand Up @@ -51,7 +50,6 @@ export function Provider ({
const [events, setEvents] = useState<EventTarget>()
const [accounts, setAccounts] = useState<Account[]>([])
const [spaces, setSpaces] = useState<Space[]>([])
const [store, setStore] = useState<Store>()

useEffect(() => {
if ((client === undefined) || (events === undefined)) return
Expand All @@ -68,26 +66,25 @@ export function Provider ({
}, [client, events])

const setupClient = async (): Promise<void> => {
const { client, events, store } = await createClient({ servicePrincipal, connection })
const { client, events } = await createClient({ servicePrincipal, connection })
setClient(client)
setEvents(events)
setAccounts(Object.values(client.accounts()))
setSpaces(client.spaces())
setStore(store)
}

const logout = async (): Promise<void> => {
if (store !== undefined) {
await store.reset()
// set state back to defaults
setClient(undefined)
setEvents(undefined)
setAccounts([])
setSpaces([])
setStore(undefined)
// try to set state up again
await setupClient()
}
// it's possible that setupClient hasn't been run yet - run createClient here
// to get a reliable handle on the latest store
const { store } = await createClient({ servicePrincipal, connection })
await store.reset()
// set state back to defaults
setClient(undefined)
setEvents(undefined)
setAccounts([])
setSpaces([])
// set state up again
await setupClient()
}

useEffect(() => { void setupClient() }, []) // load client - once.
Expand Down
27 changes: 2 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eb52faf

Please sign in to comment.