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"
},
"dependencies": {
"@atproto/api": "^0.7.4",
"@atproto/api": "^0.8.0",
"@bam.tech/react-native-image-resizer": "^3.0.4",
"@braintree/sanitize-url": "^6.0.2",
"@emoji-mart/react": "^1.1.1",

View File

@ -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']>(

View File

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