Persist PDS URL for session resumption (#3620)

* fix: store PDS URL for session resumption

* fix: handle a few more cases

* fix: blocking resumption should also use pds url

* refactor: do it in the construct itself

* fix: revert ce96223
zio/stable
Mary 2024-04-25 03:57:16 +07:00 committed by GitHub
parent 05212ca9e3
commit 15055cb8c4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 4 deletions

View File

@ -15,6 +15,7 @@ const accountSchema = z.object({
refreshJwt: z.string().optional(), // optional because it can expire refreshJwt: z.string().optional(), // optional because it can expire
accessJwt: z.string().optional(), // optional because it can expire accessJwt: z.string().optional(), // optional because it can expire
deactivated: z.boolean().optional(), deactivated: z.boolean().optional(),
pdsUrl: z.string().optional(),
}) })
export type PersistedAccount = z.infer<typeof accountSchema> export type PersistedAccount = z.infer<typeof accountSchema>

View File

@ -117,6 +117,7 @@ const ApiContext = React.createContext<ApiContext>({
}) })
function createPersistSessionHandler( function createPersistSessionHandler(
agent: BskyAgent,
account: SessionAccount, account: SessionAccount,
persistSessionCallback: (props: { persistSessionCallback: (props: {
expired: boolean expired: boolean
@ -144,6 +145,7 @@ function createPersistSessionHandler(
email: session?.email || account.email, email: session?.email || account.email,
emailConfirmed: session?.emailConfirmed || account.emailConfirmed, emailConfirmed: session?.emailConfirmed || account.emailConfirmed,
deactivated: isSessionDeactivated(session?.accessJwt), deactivated: isSessionDeactivated(session?.accessJwt),
pdsUrl: agent.pdsUrl?.toString(),
/* /*
* Tokens are undefined if the session expires, or if creation fails for * Tokens are undefined if the session expires, or if creation fails for
@ -276,12 +278,14 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
refreshJwt: agent.session.refreshJwt, refreshJwt: agent.session.refreshJwt,
accessJwt: agent.session.accessJwt, accessJwt: agent.session.accessJwt,
deactivated, deactivated,
pdsUrl: agent.pdsUrl?.toString(),
} }
await configureModeration(agent, account) await configureModeration(agent, account)
agent.setPersistSessionHandler( agent.setPersistSessionHandler(
createPersistSessionHandler( createPersistSessionHandler(
agent,
account, account,
({expired, refreshedAccount}) => { ({expired, refreshedAccount}) => {
upsertAccount(refreshedAccount, expired) upsertAccount(refreshedAccount, expired)
@ -327,12 +331,14 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
refreshJwt: agent.session.refreshJwt, refreshJwt: agent.session.refreshJwt,
accessJwt: agent.session.accessJwt, accessJwt: agent.session.accessJwt,
deactivated: isSessionDeactivated(agent.session.accessJwt), deactivated: isSessionDeactivated(agent.session.accessJwt),
pdsUrl: agent.pdsUrl?.toString(),
} }
await configureModeration(agent, account) await configureModeration(agent, account)
agent.setPersistSessionHandler( agent.setPersistSessionHandler(
createPersistSessionHandler( createPersistSessionHandler(
agent,
account, account,
({expired, refreshedAccount}) => { ({expired, refreshedAccount}) => {
upsertAccount(refreshedAccount, expired) upsertAccount(refreshedAccount, expired)
@ -379,16 +385,24 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
logger.debug(`session: initSession`, {}, logger.DebugContext.session) logger.debug(`session: initSession`, {}, logger.DebugContext.session)
const fetchingGates = tryFetchGates(account.did, 'prefer-low-latency') const fetchingGates = tryFetchGates(account.did, 'prefer-low-latency')
const agent = new BskyAgent({ const agent = new BskyAgent({service: account.service})
service: account.service,
persistSession: createPersistSessionHandler( // restore the correct PDS URL if available
if (account.pdsUrl) {
agent.pdsUrl = agent.api.xrpc.uri = new URL(account.pdsUrl)
}
agent.setPersistSessionHandler(
createPersistSessionHandler(
agent,
account, account,
({expired, refreshedAccount}) => { ({expired, refreshedAccount}) => {
upsertAccount(refreshedAccount, expired) upsertAccount(refreshedAccount, expired)
}, },
{networkErrorCallback: clearCurrentAccount}, {networkErrorCallback: clearCurrentAccount},
), ),
}) )
// @ts-ignore // @ts-ignore
if (IS_DEV && isWeb) window.agent = agent if (IS_DEV && isWeb) window.agent = agent
await configureModeration(agent, account) await configureModeration(agent, account)
@ -421,6 +435,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
logger.debug(`session: attempting to reuse previous session`) logger.debug(`session: attempting to reuse previous session`)
agent.session = prevSession agent.session = prevSession
__globalAgent = agent __globalAgent = agent
await fetchingGates await fetchingGates
upsertAccount(account) upsertAccount(account)
@ -498,6 +513,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
refreshJwt: agent.session.refreshJwt, refreshJwt: agent.session.refreshJwt,
accessJwt: agent.session.accessJwt, accessJwt: agent.session.accessJwt,
deactivated: isSessionDeactivated(agent.session.accessJwt), deactivated: isSessionDeactivated(agent.session.accessJwt),
pdsUrl: agent.pdsUrl?.toString(),
} }
} }
}, },