diff --git a/packages/auth/src/RocketChatAuth.ts b/packages/auth/src/RocketChatAuth.ts index 0f2c55f196..94c901925f 100644 --- a/packages/auth/src/RocketChatAuth.ts +++ b/packages/auth/src/RocketChatAuth.ts @@ -36,7 +36,9 @@ class RocketChatAuth { async onAuthChange(callback: (user: object | null) => void) { this.authListeners.push(callback); const user = await this.getCurrentUser(); - callback(user); + if (this.authListeners.includes(callback)) { + callback(user); + } } async removeAuthListener(callback: (user: object | null) => void) { diff --git a/packages/react/src/views/ChatBody/ChatBody.js b/packages/react/src/views/ChatBody/ChatBody.js index 34f5c8bf40..1c94b64f1c 100644 --- a/packages/react/src/views/ChatBody/ChatBody.js +++ b/packages/react/src/views/ChatBody/ChatBody.js @@ -146,16 +146,19 @@ const ChatBody = ({ ); useEffect(() => { - RCInstance.auth.onAuthChange((user) => { + const handleAuthChange = (user) => { if (user) { RCInstance.addMessageListener(addMessage); RCInstance.addMessageDeleteListener(removeMessage); RCInstance.addActionTriggeredListener(onActionTriggerResponse); RCInstance.addUiInteractionListener(onActionTriggerResponse); } - }); + }; + + RCInstance.auth.onAuthChange(handleAuthChange); return () => { + RCInstance.auth.removeAuthListener(handleAuthChange); RCInstance.removeMessageListener(addMessage); RCInstance.removeMessageDeleteListener(removeMessage); RCInstance.removeActionTriggeredListener(onActionTriggerResponse); @@ -164,24 +167,36 @@ const ChatBody = ({ }, [RCInstance, addMessage, removeMessage, onActionTriggerResponse]); useEffect(() => { - RCInstance.auth.onAuthChange((user) => { + const handleAuthChange = (user) => { if (user) { getMessagesAndRoles(); setHasMoreMessages(true); } else { getMessagesAndRoles(anonymousMode); } - }); + }; + + RCInstance.auth.onAuthChange(handleAuthChange); + + return () => { + RCInstance.auth.removeAuthListener(handleAuthChange); + }; }, [RCInstance, anonymousMode, getMessagesAndRoles]); useEffect(() => { - RCInstance.auth.onAuthChange((user) => { + const handleAuthChange = (user) => { if (user) { fetchAndSetPermissions(); } else { permissionsRef.current = null; } - }); + }; + + RCInstance.auth.onAuthChange(handleAuthChange); + + return () => { + RCInstance.auth.removeAuthListener(handleAuthChange); + }; }, []); // Expose clearUnreadDivider function via ref for ChatInput to call diff --git a/packages/react/src/views/ChatInput/ChatInput.js b/packages/react/src/views/ChatInput/ChatInput.js index e753b689ae..a868871c5f 100644 --- a/packages/react/src/views/ChatInput/ChatInput.js +++ b/packages/react/src/views/ChatInput/ChatInput.js @@ -144,7 +144,7 @@ const ChatInput = ({ scrollToBottom, clearUnreadDividerRef }) => { ); useEffect(() => { - RCInstance.auth.onAuthChange((user) => { + const handleAuthChange = (user) => { if (user) { RCInstance.getCommandsList() .then((data) => setCommands(data.commands || [])) @@ -156,7 +156,13 @@ const ChatInput = ({ scrollToBottom, clearUnreadDividerRef }) => { ) .catch(console.error); } - }); + }; + + RCInstance.auth.onAuthChange(handleAuthChange); + + return () => { + RCInstance.auth.removeAuthListener(handleAuthChange); + }; }, [RCInstance, isChannelPrivate, setMembersHandler]); useEffect(() => { diff --git a/packages/react/src/views/EmbeddedChat.js b/packages/react/src/views/EmbeddedChat.js index f3b94c7b48..ed3285d2b6 100644 --- a/packages/react/src/views/EmbeddedChat.js +++ b/packages/react/src/views/EmbeddedChat.js @@ -134,7 +134,7 @@ const EmbeddedChat = (props) => { }, [RCInstance, auth, setIsLoginIn]); useEffect(() => { - RCInstance.auth.onAuthChange((user) => { + const handleAuthChange = (user) => { if (user) { RCInstance.connect() .then(() => { @@ -151,7 +151,13 @@ const EmbeddedChat = (props) => { } else { setIsUserAuthenticated(false); } - }); + }; + + RCInstance.auth.onAuthChange(handleAuthChange); + + return () => { + RCInstance.auth.removeAuthListener(handleAuthChange); + }; }, [ RCInstance, setAuthenticatedName,