diff --git a/src/state/session/index.tsx b/src/state/session/index.tsx index b5a985e67d..af8417f8d7 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 (