Skip to content

Commit

Permalink
feat: add a logout function
Browse files Browse the repository at this point in the history
this resets the store, which isn't exposed in the client's types. I think it's a little better to manage the store manually anyway since we're going to be calling a method on it directly, so I'm happy with this but could be dissuaded.
  • Loading branch information
travis committed Nov 29, 2023
1 parent 65ace80 commit 82e4ccd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
13 changes: 10 additions & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { Client, create as createW3UPClient } from '@web3-storage/w3up-client'
import { Account } from '@web3-storage/w3up-client/account'
import { Space } from '@web3-storage/w3up-client/space'
import { createServiceConf } from './service'
import { Driver } from '@web3-storage/access/drivers/types'

export * from '@web3-storage/w3up-client/types'
export { Client, Account, Space, ServiceConfig }
export type Store = Driver<AgentDataExport>

const DB_NAME = '@w3ui'
const DB_STORE_NAME = 'core'
Expand All @@ -29,7 +31,12 @@ export interface ContextState {
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface ContextActions {}
export interface ContextActions {
/**
* Reset local store (deleting existing agent), logging the user out.
*/
logout: () => Promise<void>
}

export interface CreateClientOptions extends ServiceConfig {
events?: EventTarget
Expand Down Expand Up @@ -59,11 +66,11 @@ class IndexedDBEventDispatcherStore extends StoreIndexedDB {
*/
export async function createClient (
options?: CreateClientOptions
): Promise<{ client: Client, events: EventTarget }> {
): Promise<{ client: Client, events: EventTarget, store: Store }> {
const dbName = `${DB_NAME}${options?.servicePrincipal != null ? '@' + options?.servicePrincipal.did() : ''}`
const events = options?.events ?? new EventTarget()
const store = new IndexedDBEventDispatcherStore(dbName, events)
const serviceConf = createServiceConf(options)
const client = await createW3UPClient({ store, serviceConf })
return { client, events }
return { client, events, store }
}
37 changes: 25 additions & 12 deletions packages/react/src/providers/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import type {
ContextActions,
ServiceConfig,
Space,
Account
Account,
Store
} from '@w3ui/core'

import React, { createContext, useState, useContext, useEffect, ReactNode } from 'react'
Expand Down Expand Up @@ -46,6 +47,7 @@ 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 @@ -61,22 +63,33 @@ export function Provider ({
}
}, [client, events])

const getClient = async (): Promise<Client> => {
if (client == null) {
const { client, events } = await createClient({ servicePrincipal, connection })
setClient(client)
setEvents(events)
setAccounts(Object.values(client.accounts()))
setSpaces(client.spaces())
return client
const setupClient = async (): Promise<void> => {
const { client, events, store } = 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) {
await store.reset()
// set state back to defaults
setClient(undefined)
setEvents(undefined)
setAccounts([])
setSpaces([])
setStore(undefined)
// try to set state up again
await setupClient()
}
return client
}

useEffect(() => { void getClient() }, []) // load client - once.
useEffect(() => { setupClient() }, []) // load client - once.

return (
<Context.Provider value={[{ client, accounts, spaces }, {}]}>
<Context.Provider value={[{ client, accounts, spaces }, { logout }]}>
{children}
</Context.Provider>
)
Expand Down

0 comments on commit 82e4ccd

Please sign in to comment.