Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a logout function #595

Merged
merged 6 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 }
}
4 changes: 4 additions & 0 deletions packages/react/src/Authenticator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export const AuthenticatorContextDefaultValue: AuthenticatorContextValue = [
},
cancelLogin: () => {
throw new Error('missing cancel login function')
},
logout: () => {
throw new Error('missing logout function')
}
}
]
Expand Down Expand Up @@ -112,6 +115,7 @@ export const AuthenticatorRoot: Component<AuthenticatorRootProps> =
() => [
{ ...state, email, submitted, handleRegisterSubmit },
{
...actions,
setEmail,
cancelLogin: () => {
loginAbortController?.abort()
Expand Down
46 changes: 30 additions & 16 deletions packages/react/src/providers/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
Account
} from '@w3ui/core'

import React, { createContext, useState, useContext, useEffect } from 'react'
import React, { createContext, useState, useContext, useEffect, ReactNode } from 'react'
import { createClient } from '@w3ui/core'

export { ContextState, ContextActions }
Expand All @@ -23,15 +23,19 @@ export const ContextDefaultValue: ContextValue = [
accounts: [],
spaces: []
},
{}
{
logout: async () => {
throw new Error('missing logout function')
}
}
]

export const Context = createContext<ContextValue>(
ContextDefaultValue
)

export interface ProviderProps extends ServiceConfig {
children?: JSX.Element
children?: ReactNode
}

/**
Expand All @@ -41,7 +45,7 @@ export function Provider ({
children,
servicePrincipal,
connection
}: ProviderProps): JSX.Element {
}: ProviderProps): ReactNode {
const [client, setClient] = useState<Client>()
const [events, setEvents] = useState<EventTarget>()
const [accounts, setAccounts] = useState<Account[]>([])
Expand All @@ -61,22 +65,32 @@ 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
}
return client
const setupClient = async (): Promise<void> => {
const { client, events } = await createClient({ servicePrincipal, connection })
setClient(client)
setEvents(events)
setAccounts(Object.values(client.accounts()))
setSpaces(client.spaces())
}

const logout = async (): Promise<void> => {
// 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 getClient() }, []) // load client - once.
useEffect(() => { void setupClient() }, []) // load client - once.
Copy link
Contributor

@gobengo gobengo Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if props.connection changes, wouldn't we want this to run again? so that a new client gets created and passed to setClient?

maybe setupClient assignment should use React.useCallback with appropriate dependencies, and this useEffect can depend on the variables it depends on e.g. setupClient

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also maybe we should have the lint rule for react hook deps? https://github.com/facebook/react/blob/main/packages/eslint-plugin-react-hooks/README.md

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I think you're right! I think that's orthogonal to the purpose of this PR though - happy to file an issue and even work on it today, but want to get this PR merged and shipped to fix the regression in production

Copy link
Contributor

@gobengo gobengo Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
useEffect(() => { void setupClient() }, []) // load client - once.
useEffect(() => { void setupClient() }, [servicePrincipal, connection, setupClient])

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmmm, not sure about this - on at the very least needs an update to the comment to explain why we're doing something so unusual, and I'm pretty sure this would trigger the linter you suggested above (though tbh I'm not a huge fan of that linter since it is not always correct and triggers on some valid use-cases)


return (
<Context.Provider value={[{ client, accounts, spaces }, {}]}>
<Context.Provider value={[{ client, accounts, spaces }, { logout }]}>
{children}
</Context.Provider>
)
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.

Loading