Strip back

This commit is contained in:
Eric Bailey
2024-04-03 17:18:53 -05:00
parent a49a5a351d
commit cd5b555b08
+25 -92
View File
@@ -12,12 +12,10 @@ import {IS_TEST_USER} from '#/lib/constants'
import {logEvent, LogEvents} from '#/lib/statsig/statsig' import {logEvent, LogEvents} from '#/lib/statsig/statsig'
import {hasProp} from '#/lib/type-guards' import {hasProp} from '#/lib/type-guards'
import {logger} from '#/logger' import {logger} from '#/logger'
import {isWeb} from '#/platform/detection'
import * as persisted from '#/state/persisted' import * as persisted from '#/state/persisted'
import {PUBLIC_BSKY_AGENT} from '#/state/queries' import {PUBLIC_BSKY_AGENT} from '#/state/queries'
import {useLoggedOutViewControls} from '#/state/shell/logged-out' import {useLoggedOutViewControls} from '#/state/shell/logged-out'
import {useCloseAllActiveElements} from '#/state/util' import {useCloseAllActiveElements} from '#/state/util'
import {IS_DEV} from '#/env'
import {emitSessionDropped} from '../events' import {emitSessionDropped} from '../events'
import {readLabelers} from './agent-config' import {readLabelers} from './agent-config'
@@ -328,8 +326,6 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
) )
__globalAgent = agent __globalAgent = agent
// @ts-ignore
if (IS_DEV && isWeb) window.agent = agent
upsertAccount(account) upsertAccount(account)
logger.debug(`session: logged in`, {}, logger.DebugContext.session) logger.debug(`session: logged in`, {}, logger.DebugContext.session)
@@ -367,15 +363,23 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
service: account.service, service: account.service,
persistSession: createPersistSessionHandler( persistSession: createPersistSessionHandler(
account, account,
({expired, refreshedAccount}) => { async ({expired, refreshedAccount}) => {
__globalAgent = agent
await configureModeration(agent, account)
upsertAccount(refreshedAccount, expired) upsertAccount(refreshedAccount, expired)
}, },
{networkErrorCallback: clearCurrentAccount}, {networkErrorCallback: clearCurrentAccount},
), ),
}) })
// @ts-ignore
if (IS_DEV && isWeb) window.agent = agent const prevSession = {
await configureModeration(agent, account) accessJwt: account.accessJwt || '',
refreshJwt: account.refreshJwt || '',
did: account.did,
handle: account.handle,
deactivated:
isSessionDeactivated(account.accessJwt) || account.deactivated,
}
let canReusePrevSession = false let canReusePrevSession = false
try { try {
@@ -392,94 +396,23 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
logger.error(`session: could not decode jwt`) logger.error(`session: could not decode jwt`)
} }
const prevSession = {
accessJwt: account.accessJwt || '',
refreshJwt: account.refreshJwt || '',
did: account.did,
handle: account.handle,
deactivated:
isSessionDeactivated(account.accessJwt) || account.deactivated,
}
if (canReusePrevSession) { if (canReusePrevSession) {
logger.debug(`session: attempting to reuse previous session`) logger.debug(
`session: attempting to reuse previous session`,
{},
logger.DebugContext.session,
)
agent.session = prevSession agent.session = prevSession
__globalAgent = agent __globalAgent = agent
await configureModeration(agent, account)
upsertAccount(account) upsertAccount(account)
if (prevSession.deactivated) {
// don't attempt to resume
// use will be taken to the deactivated screen
logger.debug(`session: reusing session for deactivated account`)
return
}
// Intentionally not awaited to unblock the UI:
resumeSessionWithFreshAccount()
.then(freshAccount => {
if (JSON.stringify(account) !== JSON.stringify(freshAccount)) {
logger.info(
`session: reuse of previous session returned a fresh account, upserting`,
)
upsertAccount(freshAccount)
}
})
.catch(e => {
/*
* Note: `agent.persistSession` is also called when this fails, and
* we handle that failure via `createPersistSessionHandler`
*/
logger.info(`session: resumeSessionWithFreshAccount failed`, {
message: e,
})
__globalAgent = PUBLIC_BSKY_AGENT
})
} else { } else {
logger.debug(`session: attempting to resume using previous session`) logger.debug(
`session: attempting to resume using previous session`,
try { {},
const freshAccount = await resumeSessionWithFreshAccount() logger.DebugContext.session,
__globalAgent = agent )
upsertAccount(freshAccount)
} catch (e) {
/*
* Note: `agent.persistSession` is also called when this fails, and
* we handle that failure via `createPersistSessionHandler`
*/
logger.info(`session: resumeSessionWithFreshAccount failed`, {
message: e,
})
__globalAgent = PUBLIC_BSKY_AGENT
}
}
async function resumeSessionWithFreshAccount(): Promise<SessionAccount> {
logger.debug(`session: resumeSessionWithFreshAccount`)
await networkRetry(1, () => agent.resumeSession(prevSession)) await networkRetry(1, () => agent.resumeSession(prevSession))
/*
* If `agent.resumeSession` fails above, it'll throw. This is just to
* make TypeScript happy.
*/
if (!agent.session) {
throw new Error(`session: initSession failed to establish a session`)
}
// ensure changes in handle/email etc are captured on reload
return {
service: agent.service.toString(),
did: agent.session.did,
handle: agent.session.handle,
email: agent.session.email,
emailConfirmed: agent.session.emailConfirmed || false,
refreshJwt: agent.session.refreshJwt,
accessJwt: agent.session.accessJwt,
deactivated: isSessionDeactivated(agent.session.accessJwt),
}
} }
}, },
[upsertAccount, clearCurrentAccount], [upsertAccount, clearCurrentAccount],
@@ -576,7 +509,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
}, [state]) }, [state])
React.useEffect(() => { React.useEffect(() => {
return persisted.onUpdate(() => { return persisted.onUpdate(async () => {
const session = persisted.get('session') const session = persisted.get('session')
logger.debug(`session: persisted onUpdate`, {}) logger.debug(`session: persisted onUpdate`, {})
@@ -594,7 +527,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
}, },
}) })
initSession(session.currentAccount) await initSession(session.currentAccount)
} else { } else {
logger.debug(`session: persisted onUpdate, updating session`, {}) logger.debug(`session: persisted onUpdate, updating session`, {})