From 8938fc87a089cc51cabcb7d69ec56d0c69bfffcb Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Thu, 23 May 2024 02:45:50 +0100 Subject: [Session] Dispose of stale agents immediately --- src/state/session/index.tsx | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src') diff --git a/src/state/session/index.tsx b/src/state/session/index.tsx index b5a985e67..af8417f8d 100644 --- a/src/state/session/index.tsx +++ b/src/state/session/index.tsx @@ -208,6 +208,19 @@ export function Provider({children}: React.PropsWithChildren<{}>) { if (IS_DEV && isWeb) window.agent = state.currentAgentState.agent const agent = state.currentAgentState.agent as BskyAgent + const currentAgentRef = React.useRef(agent) + React.useEffect(() => { + if (currentAgentRef.current !== agent) { + // Read the previous value and immediately advance the pointer. + const prevAgent = currentAgentRef.current + currentAgentRef.current = agent + // We never reuse agents so let's fully neutralize the previous one. + // This ensures it won't try to consume any refresh tokens. + prevAgent.session = undefined + prevAgent.setPersistSessionHandler(undefined) + } + }, [agent]) + return ( -- cgit 1.4.1