Bluesky app fork with some witchin' additions 💫
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Ensure sessions and tokens are synced between tabs (#2498)

* Ensure sessions and tokens are synced between tabs

* Send clear account to sentry for monitoring

authored by

Eric Bailey and committed by
GitHub
11f6ff7c c0261fc9

+37 -23
+37 -23
src/state/session/index.tsx
··· 193 193 ) 194 194 195 195 const clearCurrentAccount = React.useCallback(() => { 196 - logger.debug( 197 - `session: clear current account`, 198 - {}, 199 - logger.DebugContext.session, 200 - ) 196 + logger.warn(`session: clear current account`) 201 197 __globalAgent = PUBLIC_BSKY_AGENT 202 198 queryClient.clear() 203 199 setStateAndPersist(s => ({ ··· 322 318 ) 323 319 324 320 const logout = React.useCallback<ApiContext['logout']>(async () => { 321 + logger.info(`session: logout`) 325 322 clearCurrentAccount() 326 - logger.debug(`session: logout`, {}, logger.DebugContext.session) 327 323 setStateAndPersist(s => { 328 324 return { 329 325 ...s, ··· 551 547 return persisted.onUpdate(() => { 552 548 const session = persisted.get('session') 553 549 554 - logger.debug(`session: onUpdate`, {}, logger.DebugContext.session) 550 + logger.info(`session: persisted onUpdate`, {}) 555 551 556 - if (session.currentAccount) { 552 + if (session.currentAccount && session.currentAccount.refreshJwt) { 557 553 if (session.currentAccount?.did !== state.currentAccount?.did) { 558 - logger.debug( 559 - `session: switching account`, 560 - { 561 - from: { 562 - did: state.currentAccount?.did, 563 - handle: state.currentAccount?.handle, 564 - }, 565 - to: { 566 - did: session.currentAccount.did, 567 - handle: session.currentAccount.handle, 568 - }, 554 + logger.info(`session: persisted onUpdate, switching accounts`, { 555 + from: { 556 + did: state.currentAccount?.did, 557 + handle: state.currentAccount?.handle, 558 + }, 559 + to: { 560 + did: session.currentAccount.did, 561 + handle: session.currentAccount.handle, 569 562 }, 570 - logger.DebugContext.session, 571 - ) 563 + }) 572 564 573 565 initSession(session.currentAccount) 566 + } else { 567 + logger.info(`session: persisted onUpdate, updating session`, {}) 568 + 569 + /* 570 + * Use updated session in this tab's agent. Do not call 571 + * upsertAccount, since that will only persist the session that's 572 + * already persisted, and we'll get a loop between tabs. 573 + */ 574 + // @ts-ignore we checked for `refreshJwt` above 575 + __globalAgent.session = session.currentAccount 574 576 } 575 577 } else if (!session.currentAccount && state.currentAccount) { 576 578 logger.debug( 577 - `session: logging out`, 579 + `session: persisted onUpdate, logging out`, 578 580 { 579 581 did: state.currentAccount?.did, 580 582 handle: state.currentAccount?.handle, ··· 582 584 logger.DebugContext.session, 583 585 ) 584 586 587 + /* 588 + * No need to do a hard logout here. If we reach this, tokens for this 589 + * account have already been cleared either by an `expired` event 590 + * handled by `persistSession` (which nukes this accounts tokens only), 591 + * or by a `logout` call which nukes all accounts tokens) 592 + */ 585 593 clearCurrentAccount() 586 594 } 595 + 596 + setState(s => ({ 597 + ...s, 598 + accounts: session.accounts, 599 + currentAccount: session.currentAccount, 600 + })) 587 601 }) 588 - }, [state, clearCurrentAccount, initSession]) 602 + }, [state, setState, clearCurrentAccount, initSession]) 589 603 590 604 const stateContext = React.useMemo( 591 605 () => ({