diff options
author | Eric Bailey <git@esb.lol> | 2024-01-08 17:59:55 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-08 15:59:55 -0800 |
commit | 0ee0554b8632a9d32fa36ffa9fde8d719ab1527e (patch) | |
tree | 520427b516f8fbd25e9875338ef04b1b8a70dfe8 /src | |
parent | c1e8abfb77e2fa4b01e52855eec11f7fb53c32f5 (diff) | |
download | voidsky-0ee0554b8632a9d32fa36ffa9fde8d719ab1527e.tar.zst |
Do not nuke session on unknown backend errors (#2399)
* Do not nuke session on unknown backend errors * Restore existing functionality * Use new event * Kick user out to sign in * Remove unstable
Diffstat (limited to 'src')
-rw-r--r-- | src/state/session/index.tsx | 66 |
1 files changed, 43 insertions, 23 deletions
diff --git a/src/state/session/index.tsx b/src/state/session/index.tsx index a452d6aa6..17f25570e 100644 --- a/src/state/session/index.tsx +++ b/src/state/session/index.tsx @@ -102,10 +102,21 @@ function createPersistSessionHandler( expired: boolean refreshedAccount: SessionAccount }) => void, + { + networkErrorCallback, + }: { + networkErrorCallback?: () => void + } = {}, ): AtpPersistSessionHandler { return function persistSession(event, session) { const expired = event === 'expired' || event === 'create-failed' + if (event === 'network-error') { + logger.warn(`session: persistSessionHandler received network-error event`) + networkErrorCallback?.() + return + } + const refreshedAccount: SessionAccount = { service: account.service, did: session?.did || account.did, @@ -179,6 +190,20 @@ export function Provider({children}: React.PropsWithChildren<{}>) { [setStateAndPersist], ) + 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 createAccount = React.useCallback<ApiContext['createAccount']>( async ({service, email, password, handle, inviteCode}: any) => { logger.info(`session: creating account`, { @@ -211,9 +236,13 @@ export function Provider({children}: React.PropsWithChildren<{}>) { } agent.setPersistSessionHandler( - createPersistSessionHandler(account, ({expired, refreshedAccount}) => { - upsertAccount(refreshedAccount, expired) - }), + createPersistSessionHandler( + account, + ({expired, refreshedAccount}) => { + upsertAccount(refreshedAccount, expired) + }, + {networkErrorCallback: clearCurrentAccount}, + ), ) __globalAgent = agent @@ -230,7 +259,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { ) track('Create Account') }, - [upsertAccount, queryClient], + [upsertAccount, queryClient, clearCurrentAccount], ) const login = React.useCallback<ApiContext['login']>( @@ -263,9 +292,13 @@ export function Provider({children}: React.PropsWithChildren<{}>) { } agent.setPersistSessionHandler( - createPersistSessionHandler(account, ({expired, refreshedAccount}) => { - upsertAccount(refreshedAccount, expired) - }), + createPersistSessionHandler( + account, + ({expired, refreshedAccount}) => { + upsertAccount(refreshedAccount, expired) + }, + {networkErrorCallback: clearCurrentAccount}, + ), ) __globalAgent = agent @@ -283,23 +316,9 @@ export function Provider({children}: React.PropsWithChildren<{}>) { track('Sign In', {resumedSession: false}) }, - [upsertAccount, queryClient], + [upsertAccount, queryClient, clearCurrentAccount], ) - 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 () => { clearCurrentAccount() logger.debug(`session: logout`, {}, logger.DebugContext.session) @@ -333,6 +352,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { ({expired, refreshedAccount}) => { upsertAccount(refreshedAccount, expired) }, + {networkErrorCallback: clearCurrentAccount}, ), }) @@ -433,7 +453,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { } } }, - [upsertAccount, queryClient], + [upsertAccount, queryClient, clearCurrentAccount], ) const resumeSession = React.useCallback<ApiContext['resumeSession']>( |