11import * as sdk from "matrix-js-sdk" ;
22import { IChatProvider } from "./IChatProvider" ;
3- import { RocketChatAuth } from "@embeddedchat/auth" ;
3+ import { RocketChatAuth , IRocketChatAuthOptions } from "@embeddedchat/auth" ;
44
55class MatrixAuth extends RocketChatAuth {
66 private onAuthChangeCb : ( ( user : any ) => void ) | null = null ;
77 currentUser : any | null = null ;
88
9- constructor ( config : any ) {
9+ constructor ( config : IRocketChatAuthOptions ) {
1010 super ( config ) ;
1111 }
12-
13- async onAuthChange ( cb : ( user : any ) => void ) : Promise < void > {
14- this . onAuthChangeCb = cb ;
15- if ( this . currentUser ) {
16- cb ( this . currentUser ) ;
17- }
18- }
19-
20- notifyAuthChange ( user : any ) {
21- this . currentUser = user ;
22- if ( this . onAuthChangeCb ) {
23- this . onAuthChangeCb ( user ) ;
24- }
25- }
2612}
2713
2814export default class MatrixProvider implements IChatProvider {
@@ -32,14 +18,18 @@ export default class MatrixProvider implements IChatProvider {
3218 private auth : MatrixAuth ;
3319 private onMessageCallbacks : ( ( message : any ) => void ) [ ] = [ ] ;
3420
35- constructor ( host : string , roomId : string ) {
21+ constructor (
22+ host : string ,
23+ roomId : string ,
24+ { getToken, saveToken, deleteToken } : IRocketChatAuthOptions
25+ ) {
3626 this . host = host ;
3727 this . roomId = roomId ;
3828 this . auth = new MatrixAuth ( {
3929 host : this . host ,
40- deleteToken : async ( ) => { } ,
41- getToken : async ( ) => "" ,
42- saveToken : async ( ) => { } ,
30+ deleteToken,
31+ getToken,
32+ saveToken,
4333 } ) ;
4434 }
4535
@@ -171,41 +161,34 @@ export default class MatrixProvider implements IChatProvider {
171161 password : string ,
172162 code ?: string
173163 ) : Promise < any > {
174- if ( ! this . client ) {
164+ try {
165+ const credentials = code
166+ ? { user : userOrEmail , password, code }
167+ : { user : userOrEmail , password } ;
168+
169+ const data = await this . auth . loginWithPassword ( credentials ) ;
170+
171+ if ( ! data ) {
172+ throw new Error ( "Login failed" ) ;
173+ }
174+
175+ const { authToken, userId } = data ;
176+
177+ // Initialize Matrix client with RC auth token
175178 this . client = sdk . createClient ( {
176179 baseUrl : this . host ,
177- } ) ;
178- }
179-
180- try {
181- const response = await this . client . login ( "m.login.password" , {
182- identifier : {
183- type : "m.id.user" ,
184- user : userOrEmail ,
185- } ,
186- password : password ,
180+ accessToken : authToken ,
181+ userId : userId ,
187182 } ) ;
188183
189184 // Ensure we connect (start client and sync) after login
190185 await this . connect ( ) ;
191186
192- const user = {
193- username : response . user_id ,
194- _id : response . user_id ,
195- name : response . user_id ,
196- avatarUrl : "" , // Placeholder
197- roles : [ ] ,
198- } ;
199-
200- // Notify auth change to update UI
201- this . auth . notifyAuthChange ( { me : user } ) ;
202-
203- return { status : "success" , me : user } ;
187+ return { status : "success" , me : data } ;
204188 } catch ( error : any ) {
205- // Return error in Rocket.Chat format for toast notifications
206- console . error ( "Matrix login failed:" , error ) ;
189+ console . error ( "MatrixProvider login failed:" , error ) ;
207190 return {
208- error : error . httpStatus === 403 ? 403 : "Unauthorized" ,
191+ error : "Unauthorized" ,
209192 message : error . message || "Invalid username or password" ,
210193 } ;
211194 }
@@ -440,21 +423,21 @@ export default class MatrixProvider implements IChatProvider {
440423 ) ;
441424 }
442425
443- addMessageDeleteListener ( callback : ( messageId : string ) => void ) : void { }
426+ addMessageDeleteListener ( callback : ( messageId : string ) => void ) : void { }
444427
445- removeMessageDeleteListener ( callback : ( messageId : string ) => void ) : void { }
428+ removeMessageDeleteListener ( callback : ( messageId : string ) => void ) : void { }
446429
447- addTypingStatusListener ( callback : ( users : string [ ] ) => void ) : void { }
430+ addTypingStatusListener ( callback : ( users : string [ ] ) => void ) : void { }
448431
449- removeTypingStatusListener ( callback : ( users : string [ ] ) => void ) : void { }
432+ removeTypingStatusListener ( callback : ( users : string [ ] ) => void ) : void { }
450433
451- addActionTriggeredListener ( callback : ( data : any ) => void ) : void { }
434+ addActionTriggeredListener ( callback : ( data : any ) => void ) : void { }
452435
453- removeActionTriggeredListener ( callback : ( data : any ) => void ) : void { }
436+ removeActionTriggeredListener ( callback : ( data : any ) => void ) : void { }
454437
455- addUiInteractionListener ( callback : ( data : any ) => void ) : void { }
438+ addUiInteractionListener ( callback : ( data : any ) => void ) : void { }
456439
457- removeUiInteractionListener ( callback : ( data : any ) => void ) : void { }
440+ removeUiInteractionListener ( callback : ( data : any ) => void ) : void { }
458441
459442 async logout ( ) : Promise < void > {
460443 if ( this . client ) {
@@ -466,9 +449,47 @@ export default class MatrixProvider implements IChatProvider {
466449 async autoLogin ( auth : {
467450 flow : "PASSWORD" | "OAUTH" | "TOKEN" ;
468451 credentials : any ;
469- } ) : Promise < void > { }
452+ } ) : Promise < void > {
453+ try {
454+ if ( ! auth || ! auth . flow ) {
455+ return ;
456+ }
457+ let user = null ;
458+ switch ( auth . flow ) {
459+ case "PASSWORD" :
460+ case "OAUTH" :
461+ // Load stored token
462+ await this . auth . load ( ) ;
463+ user = await this . auth . getCurrentUser ( ) ;
464+ break ;
465+ case "TOKEN" :
466+ if ( ! auth . credentials ) {
467+ return ;
468+ }
469+ await this . auth . loginWithOAuthServiceToken ( auth . credentials ) ;
470+ user = await this . auth . getCurrentUser ( ) ;
471+ break ;
472+ default :
473+ break ;
474+ }
475+
476+ if ( user && user . authToken && user . userId ) {
477+ if ( this . client ) {
478+ this . client . stopClient ( ) ;
479+ }
480+ this . client = sdk . createClient ( {
481+ baseUrl : this . host ,
482+ accessToken : user . authToken ,
483+ userId : user . userId ,
484+ } ) ;
485+ await this . connect ( ) ;
486+ }
487+ } catch ( error ) {
488+ console . error ( "MatrixProvider auto-login failed:" , error ) ;
489+ }
490+ }
470491
471- async googleSSOLogin ( signIn : Function , acsCode : string ) : Promise < any > { }
492+ async googleSSOLogin ( signIn : Function , acsCode : string ) : Promise < any > { }
472493
473494 async getRCAppInfo ( ) : Promise < any > {
474495 return null ;
0 commit comments