-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ensure sessions and tokens are synced between tabs #2498
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -193,11 +193,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { | |
) | ||
|
||
const clearCurrentAccount = React.useCallback(() => { | ||
logger.debug( | ||
`session: clear current account`, | ||
{}, | ||
logger.DebugContext.session, | ||
) | ||
logger.warn(`session: clear current account`) | ||
__globalAgent = PUBLIC_BSKY_AGENT | ||
queryClient.clear() | ||
setStateAndPersist(s => ({ | ||
|
@@ -322,8 +318,8 @@ export function Provider({children}: React.PropsWithChildren<{}>) { | |
) | ||
|
||
const logout = React.useCallback<ApiContext['logout']>(async () => { | ||
logger.info(`session: logout`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will be included in any |
||
clearCurrentAccount() | ||
logger.debug(`session: logout`, {}, logger.DebugContext.session) | ||
setStateAndPersist(s => { | ||
return { | ||
...s, | ||
|
@@ -551,41 +547,59 @@ export function Provider({children}: React.PropsWithChildren<{}>) { | |
return persisted.onUpdate(() => { | ||
const session = persisted.get('session') | ||
|
||
logger.debug(`session: onUpdate`, {}, logger.DebugContext.session) | ||
logger.info(`session: persisted onUpdate`, {}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sending these to Sentry as breadcrumbs so we know if a broadcast resulted in a logout |
||
|
||
if (session.currentAccount) { | ||
if (session.currentAccount && session.currentAccount.refreshJwt) { | ||
if (session.currentAccount?.did !== state.currentAccount?.did) { | ||
logger.debug( | ||
`session: switching account`, | ||
{ | ||
from: { | ||
did: state.currentAccount?.did, | ||
handle: state.currentAccount?.handle, | ||
}, | ||
to: { | ||
did: session.currentAccount.did, | ||
handle: session.currentAccount.handle, | ||
}, | ||
logger.info(`session: persisted onUpdate, switching accounts`, { | ||
from: { | ||
did: state.currentAccount?.did, | ||
handle: state.currentAccount?.handle, | ||
}, | ||
logger.DebugContext.session, | ||
) | ||
to: { | ||
did: session.currentAccount.did, | ||
handle: session.currentAccount.handle, | ||
}, | ||
}) | ||
|
||
initSession(session.currentAccount) | ||
} else { | ||
logger.info(`session: persisted onUpdate, updating session`, {}) | ||
|
||
/* | ||
* Use updated session in this tab's agent. Do not call | ||
* upsertAccount, since that will only persist the session that's | ||
* already persisted, and we'll get a loop between tabs. | ||
*/ | ||
// @ts-ignore we checked for `refreshJwt` above | ||
__globalAgent.session = session.currentAccount | ||
} | ||
} else if (!session.currentAccount && state.currentAccount) { | ||
logger.debug( | ||
`session: logging out`, | ||
`session: persisted onUpdate, logging out`, | ||
{ | ||
did: state.currentAccount?.did, | ||
handle: state.currentAccount?.handle, | ||
}, | ||
logger.DebugContext.session, | ||
) | ||
|
||
/* | ||
* No need to do a hard logout here. If we reach this, tokens for this | ||
* account have already been cleared either by an `expired` event | ||
* handled by `persistSession` (which nukes this accounts tokens only), | ||
* or by a `logout` call which nukes all accounts tokens) | ||
*/ | ||
clearCurrentAccount() | ||
} | ||
|
||
setState(s => ({ | ||
...s, | ||
accounts: session.accounts, | ||
currentAccount: session.currentAccount, | ||
})) | ||
Comment on lines
+596
to
+600
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't directly access tokens anywhere atm, but I thought it made sense to keep each tab's React state in sync too so we don't surprise ourselves later. |
||
}) | ||
}, [state, clearCurrentAccount, initSession]) | ||
}, [state, setState, clearCurrentAccount, initSession]) | ||
|
||
const stateContext = React.useMemo( | ||
() => ({ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sending these to Sentry so we can monitor for spikes