diff --git a/lib/identity-search/identity-search-context.js b/lib/identity-search/identity-search-context.js --- a/lib/identity-search/identity-search-context.js +++ b/lib/identity-search/identity-search-context.js @@ -42,6 +42,10 @@ const socket = React.useRef(null); const heartbeatTimeoutID = React.useRef(); + const previousAuthMessage = React.useRef( + identitySearchAuthMessage, + ); + const stopHeartbeatTimeout = React.useCallback(() => { if (heartbeatTimeoutID.current) { clearTimeout(heartbeatTimeoutID.current); @@ -65,6 +69,22 @@ }, [getIdentitySearchAuthMessage]); React.useEffect(() => { + const isSocketActive = + socket.current?.readyState === WebSocket.OPEN || + socket.current?.readyState === WebSocket.CONNECTING; + + const identitySearchAuthMessageChanged = + identitySearchAuthMessage !== previousAuthMessage.current; + previousAuthMessage.current = identitySearchAuthMessage; + + if ( + (!identitySearchAuthMessage || identitySearchAuthMessageChanged) && + isSocketActive + ) { + socket.current?.close(); + return; + } + if (connected || !identitySearchAuthMessage) { return; }