@@ -142,6 +142,27 @@ const handleStopTyping = (
142
142
}
143
143
} ;
144
144
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
+
145
166
const childrenMap = {
146
167
...chatCompChildrenMap ,
147
168
visible :withDefault ( BooleanStateControl , "false" ) ,
@@ -176,13 +197,16 @@ const ChatBoxView = React.memo((
176
197
// Initialize chat manager
177
198
const modeValue = props . mode as 'local' | 'collaborative' | 'hybrid' ;
178
199
200
+ // Only initialize chat manager if userId and userName are provided
201
+ const shouldInitialize = ! ! ( props . userId . value && props . userName . value ) ;
202
+
179
203
const chatManager = useChatManager ( {
180
204
userId :props . userId . value ,
181
205
userName :props . userName . value ,
182
206
applicationId :props . applicationId . value ,
183
207
roomId :props . roomId . value ,
184
208
mode :modeValue , // Use mode from props
185
- autoConnect :true ,
209
+ autoConnect :shouldInitialize , // Only auto-connect if credentials are provided
186
210
} ) ;
187
211
188
212
useEffect ( ( ) => {
@@ -220,6 +244,21 @@ const ChatBoxView = React.memo((
220
244
}
221
245
} , [ chatManager . isConnected , props . userId . value , loadRooms ] ) ;
222
246
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
+
223
262
// Refresh joined rooms periodically
224
263
useEffect ( ( ) => {
225
264
if ( ! chatManager . isConnected ) return ;
@@ -471,6 +510,25 @@ ChatControllerComp = withMethodExposing(ChatControllerComp, [
471
510
handleSendMessage ( comp , values ?. [ 0 ] ) ;
472
511
} ,
473
512
} ,
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
+ } ,
474
532
] ) ;
475
533
476
534
ChatControllerComp = withExposingConfigs ( ChatControllerComp , [
@@ -480,6 +538,9 @@ ChatControllerComp = withExposingConfigs(ChatControllerComp, [
480
538
new NameConfig ( "participants" , trans ( "chatBox.participants" ) ) ,
481
539
new NameConfig ( "currentRoom" , trans ( "chatBox.currentRoom" ) ) ,
482
540
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" ) ) ,
483
544
new NameConfig ( "userId" , trans ( "chatBox.userId" ) ) ,
484
545
new NameConfig ( "userName" , trans ( "chatBox.userName" ) ) ,
485
546
] ) ;