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
zio/stable
Eric Bailey 2024-01-08 17:59:55 -06:00 committed by GitHub
parent c1e8abfb77
commit 0ee0554b86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 28 deletions

View File

@ -39,7 +39,7 @@
"nuke": "rm -rf ./node_modules && rm -rf ./ios && rm -rf ./android" "nuke": "rm -rf ./node_modules && rm -rf ./ios && rm -rf ./android"
}, },
"dependencies": { "dependencies": {
"@atproto/api": "^0.7.4", "@atproto/api": "^0.8.0",
"@bam.tech/react-native-image-resizer": "^3.0.4", "@bam.tech/react-native-image-resizer": "^3.0.4",
"@braintree/sanitize-url": "^6.0.2", "@braintree/sanitize-url": "^6.0.2",
"@emoji-mart/react": "^1.1.1", "@emoji-mart/react": "^1.1.1",

View File

@ -102,10 +102,21 @@ function createPersistSessionHandler(
expired: boolean expired: boolean
refreshedAccount: SessionAccount refreshedAccount: SessionAccount
}) => void, }) => void,
{
networkErrorCallback,
}: {
networkErrorCallback?: () => void
} = {},
): AtpPersistSessionHandler { ): AtpPersistSessionHandler {
return function persistSession(event, session) { return function persistSession(event, session) {
const expired = event === 'expired' || event === 'create-failed' const expired = event === 'expired' || event === 'create-failed'
if (event === 'network-error') {
logger.warn(`session: persistSessionHandler received network-error event`)
networkErrorCallback?.()
return
}
const refreshedAccount: SessionAccount = { const refreshedAccount: SessionAccount = {
service: account.service, service: account.service,
did: session?.did || account.did, did: session?.did || account.did,
@ -179,6 +190,20 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
[setStateAndPersist], [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']>( const createAccount = React.useCallback<ApiContext['createAccount']>(
async ({service, email, password, handle, inviteCode}: any) => { async ({service, email, password, handle, inviteCode}: any) => {
logger.info(`session: creating account`, { logger.info(`session: creating account`, {
@ -211,9 +236,13 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
} }
agent.setPersistSessionHandler( agent.setPersistSessionHandler(
createPersistSessionHandler(account, ({expired, refreshedAccount}) => { createPersistSessionHandler(
upsertAccount(refreshedAccount, expired) account,
}), ({expired, refreshedAccount}) => {
upsertAccount(refreshedAccount, expired)
},
{networkErrorCallback: clearCurrentAccount},
),
) )
__globalAgent = agent __globalAgent = agent
@ -230,7 +259,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
) )
track('Create Account') track('Create Account')
}, },
[upsertAccount, queryClient], [upsertAccount, queryClient, clearCurrentAccount],
) )
const login = React.useCallback<ApiContext['login']>( const login = React.useCallback<ApiContext['login']>(
@ -263,9 +292,13 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
} }
agent.setPersistSessionHandler( agent.setPersistSessionHandler(
createPersistSessionHandler(account, ({expired, refreshedAccount}) => { createPersistSessionHandler(
upsertAccount(refreshedAccount, expired) account,
}), ({expired, refreshedAccount}) => {
upsertAccount(refreshedAccount, expired)
},
{networkErrorCallback: clearCurrentAccount},
),
) )
__globalAgent = agent __globalAgent = agent
@ -283,23 +316,9 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
track('Sign In', {resumedSession: false}) 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 () => { const logout = React.useCallback<ApiContext['logout']>(async () => {
clearCurrentAccount() clearCurrentAccount()
logger.debug(`session: logout`, {}, logger.DebugContext.session) logger.debug(`session: logout`, {}, logger.DebugContext.session)
@ -333,6 +352,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
({expired, refreshedAccount}) => { ({expired, refreshedAccount}) => {
upsertAccount(refreshedAccount, expired) 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']>( const resumeSession = React.useCallback<ApiContext['resumeSession']>(

View File

@ -48,10 +48,10 @@
typed-emitter "^2.1.0" typed-emitter "^2.1.0"
zod "^3.21.4" zod "^3.21.4"
"@atproto/api@^0.7.4": "@atproto/api@^0.8.0":
version "0.7.4" version "0.8.0"
resolved "https://registry.yarnpkg.com/@atproto/api/-/api-0.7.4.tgz#0dd6e725c88d1f941c57670dc82b60fde10f4ec6" resolved "https://registry.yarnpkg.com/@atproto/api/-/api-0.8.0.tgz#57ef1f6292d05ba851e3acec575139cfc4fd7a7a"
integrity sha512-7DBy6/OcXemzCPzA0dx52LLYRABBs8bq9Docs3is+WRgEx5/Pd1kHSAlCHIaBhsym8fZ3/U4Fks/5FSHkSm4yQ== integrity sha512-FgPOoij/PAEa0YoLKqj5NFYBvysdyb13gtS2XpJOdIvUZ2KehMlTrtj7g0AR78pRfME2jJjIgmAw6qpmSsjSTw==
dependencies: dependencies:
"@atproto/common-web" "^0.2.3" "@atproto/common-web" "^0.2.3"
"@atproto/lexicon" "^0.3.1" "@atproto/lexicon" "^0.3.1"