@@ -182,15 +182,14 @@ const setupSession = async (sessionId) => {
182182 logger . error ( { sessionId, err } , 'Failed to patch WWebJS library' )
183183 } )
184184 } )
185+ initWebSocketServer ( sessionId )
186+ initializeEvents ( client , sessionId )
185187 await client . initialize ( )
186188 } catch ( error ) {
187189 logger . error ( { sessionId, err : error } , 'Initialize error' )
188190 throw error
189191 }
190192
191- initWebSocketServer ( sessionId )
192- initializeEvents ( client , sessionId )
193-
194193 // Save the session to the Map
195194 sessions . set ( sessionId , client )
196195 return { success : true , message : 'Session initiated successfully' , client }
@@ -230,13 +229,13 @@ const initializeEvents = (client, sessionId) => {
230229 } )
231230 }
232231
233- if ( isEventEnabled ( 'authenticated' ) ) {
232+ client . on ( 'authenticated' , ( ) => {
234233 client . qr = null
235- client . on ( 'authenticated' , ( ) => {
234+ if ( isEventEnabled ( 'authenticated' ) ) {
236235 triggerWebhook ( sessionWebhook , sessionId , 'authenticated' )
237236 triggerWebSocket ( sessionId , 'authenticated' )
238- } )
239- }
237+ }
238+ } )
240239
241240 if ( isEventEnabled ( 'call' ) ) {
242241 client . on ( 'call' , ( call ) => {
@@ -381,18 +380,8 @@ const initializeEvents = (client, sessionId) => {
381380 }
382381
383382 client . on ( 'qr' , ( qr ) => {
384- // by default QR code is being updated every 20 seconds
385- if ( client . qrClearTimeout ) {
386- clearTimeout ( client . qrClearTimeout )
387- }
388383 // inject qr code into session
389384 client . qr = qr
390- client . qrClearTimeout = setTimeout ( ( ) => {
391- if ( client . qr ) {
392- logger . warn ( { sessionId } , 'Removing expired QR code' )
393- client . qr = null
394- }
395- } , 30000 )
396385 if ( isEventEnabled ( 'qr' ) ) {
397386 triggerWebhook ( sessionWebhook , sessionId , 'qr' , { qr } )
398387 triggerWebSocket ( sessionId , 'qr' , { qr } )
0 commit comments