[PWI] Clarify different ways of clearing current account/logout (#1991)

* Clarify different ways of clearing current account/logout

* Reorder log
zio/stable
Eric Bailey 2023-11-24 17:41:42 -06:00 committed by GitHub
parent 20b699a008
commit e9a11114d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 17 deletions

View File

@ -42,7 +42,20 @@ export type ApiContext = {
identifier: string identifier: string
password: string password: string
}) => Promise<void> }) => Promise<void>
/**
* A full logout. Clears the `currentAccount` from session, AND removes
* access tokens from all accounts, so that returning as any user will
* require a full login.
*/
logout: () => Promise<void> logout: () => Promise<void>
/**
* A partial logout. Clears the `currentAccount` from session, but DOES NOT
* clear access tokens from accounts, allowing the user to return to their
* other accounts without logging in.
*
* Used when adding a new account, deleting an account.
*/
clearCurrentAccount: () => void
initSession: (account: SessionAccount) => Promise<void> initSession: (account: SessionAccount) => Promise<void>
resumeSession: (account?: SessionAccount) => Promise<void> resumeSession: (account?: SessionAccount) => Promise<void>
removeAccount: (account: SessionAccount) => void removeAccount: (account: SessionAccount) => void
@ -52,7 +65,6 @@ export type ApiContext = {
Pick<SessionAccount, 'handle' | 'email' | 'emailConfirmed'> Pick<SessionAccount, 'handle' | 'email' | 'emailConfirmed'>
>, >,
) => void ) => void
clearCurrentAccount: () => void
} }
const StateContext = React.createContext<StateContext>({ const StateContext = React.createContext<StateContext>({
@ -256,13 +268,26 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
[upsertAccount, queryClient], [upsertAccount, queryClient],
) )
const clearCurrentAccount = React.useCallback(() => {
logger.debug(
`session: clear current account`,
{},
logger.DebugContext.session,
)
__globalAgent = PUBLIC_BSKY_AGENT
queryClient.clear()
setStateAndPersist(s => ({
...s,
currentAccount: undefined,
}))
}, [setStateAndPersist, queryClient])
const logout = React.useCallback<ApiContext['logout']>(async () => { const logout = React.useCallback<ApiContext['logout']>(async () => {
clearCurrentAccount()
logger.debug(`session: logout`, {}, logger.DebugContext.session) logger.debug(`session: logout`, {}, logger.DebugContext.session)
setStateAndPersist(s => { setStateAndPersist(s => {
return { return {
...s, ...s,
agent: PUBLIC_BSKY_AGENT,
currentAccount: undefined,
accounts: s.accounts.map(a => ({ accounts: s.accounts.map(a => ({
...a, ...a,
refreshJwt: undefined, refreshJwt: undefined,
@ -270,7 +295,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
})), })),
} }
}) })
}, [setStateAndPersist]) }, [clearCurrentAccount, setStateAndPersist])
const initSession = React.useCallback<ApiContext['initSession']>( const initSession = React.useCallback<ApiContext['initSession']>(
async account => { async account => {
@ -404,19 +429,6 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
[setState, initSession], [setState, initSession],
) )
/**
* Clears the `currentAccount` from session. Typically used to drop the user
* back to the sign-in page.
*/
const clearCurrentAccount = React.useCallback(() => {
__globalAgent = PUBLIC_BSKY_AGENT
queryClient.clear()
setStateAndPersist(s => ({
...s,
currentAccount: undefined,
}))
}, [setStateAndPersist, queryClient])
React.useEffect(() => { React.useEffect(() => {
if (isDirty.current) { if (isDirty.current) {
isDirty.current = false isDirty.current = false