@@ -142,6 +142,27 @@ const handleStopTyping = (
142142 }
143143} ;
144144
145+ const handleJoinUser = async (
146+ comp : ConstructorToComp < typeof ChatControllerComp > ,
147+ userId : string ,
148+ userName : string ,
149+ ) => {
150+ try {
151+ // Update the component's internal state with public user credentials
152+ comp . children . userId . getView ( ) . onChange ( userId ) ;
153+ comp . children . userName . getView ( ) . onChange ( userName ) ;
154+
155+ console . log ( '[ChatController] 👤 Public user joined as:' , { userId, userName } ) ;
156+
157+ // The chat manager will automatically reconnect with new credentials
158+ // due to the useEffect that watches for userId/userName changes
159+ return true ;
160+ } catch ( error ) {
161+ console . error ( '[ChatBox] 💥 Error joining as public user:' , error ) ;
162+ return false ;
163+ }
164+ } ;
165+
145166const childrenMap = {
146167 ...chatCompChildrenMap ,
147168 visible : withDefault ( BooleanStateControl , "false" ) ,
@@ -176,13 +197,16 @@ const ChatBoxView = React.memo((
176197 // Initialize chat manager
177198 const modeValue = props . mode as 'local' | 'collaborative' | 'hybrid' ;
178199
200+ // Only initialize chat manager if userId and userName are provided
201+ const shouldInitialize = ! ! ( props . userId . value && props . userName . value ) ;
202+
179203 const chatManager = useChatManager ( {
180204 userId : props . userId . value ,
181205 userName : props . userName . value ,
182206 applicationId : props . applicationId . value ,
183207 roomId : props . roomId . value ,
184208 mode : modeValue , // Use mode from props
185- autoConnect : true ,
209+ autoConnect : shouldInitialize , // Only auto-connect if credentials are provided
186210 } ) ;
187211
188212 useEffect ( ( ) => {
@@ -220,6 +244,21 @@ const ChatBoxView = React.memo((
220244 }
221245 } , [ chatManager . isConnected , props . userId . value , loadRooms ] ) ;
222246
247+ // Handle reconnection when userId or userName changes
248+ useEffect ( ( ) => {
249+ if ( props . userId . value && props . userName . value ) {
250+ if ( chatManager . isConnected ) {
251+ // Disconnect and let the chat manager reconnect with new credentials
252+ chatManager . disconnect ( ) . then ( ( ) => {
253+ console . log ( '[ChatController] 🔄 Reconnecting with new user credentials' ) ;
254+ } ) ;
255+ } else {
256+ // If not connected and we have credentials, trigger connection
257+ console . log ( '[ChatController] 🔌 Connecting with user credentials' ) ;
258+ }
259+ }
260+ } , [ props . userId . value , props . userName . value ] ) ;
261+
223262 // Refresh joined rooms periodically
224263 useEffect ( ( ) => {
225264 if ( ! chatManager . isConnected ) return ;
@@ -471,6 +510,25 @@ ChatControllerComp = withMethodExposing(ChatControllerComp, [
471510 handleSendMessage ( comp , values ?. [ 0 ] ) ;
472511 } ,
473512 } ,
513+ {
514+ method : {
515+ name : "joinUser" ,
516+ description : "Allow users to join the chat server with their own credentials" ,
517+ params : [
518+ {
519+ name : "userId" ,
520+ type : "string" ,
521+ } ,
522+ {
523+ name : "userName" ,
524+ type : "string" ,
525+ } ,
526+ ] ,
527+ } ,
528+ execute : async ( comp : ConstructorToComp < typeof ChatControllerComp > , values : any ) => {
529+ return await handleJoinUser ( comp , values ?. [ 0 ] , values ?. [ 1 ] ) ;
530+ } ,
531+ } ,
474532] ) ;
475533
476534ChatControllerComp = withExposingConfigs ( ChatControllerComp , [
@@ -480,6 +538,9 @@ ChatControllerComp = withExposingConfigs(ChatControllerComp, [
480538 new NameConfig ( "participants" , trans ( "chatBox.participants" ) ) ,
481539 new NameConfig ( "currentRoom" , trans ( "chatBox.currentRoom" ) ) ,
482540 new NameConfig ( "typingUsers" , trans ( "chatBox.typingUsers" ) ) ,
541+ new NameConfig ( "allowRoomCreation" , trans ( "chatBox.allowRoomCreation" ) ) ,
542+ new NameConfig ( "allowRoomJoining" , trans ( "chatBox.allowRoomJoining" ) ) ,
543+ new NameConfig ( "roomPermissionMode" , trans ( "chatBox.roomPermissionMode" ) ) ,
483544 new NameConfig ( "userId" , trans ( "chatBox.userId" ) ) ,
484545 new NameConfig ( "userName" , trans ( "chatBox.userName" ) ) ,
485546] ) ;
0 commit comments