@@ -445,14 +445,46 @@ const reauthenticatePusher = _.throttle(() => {
445
445
} ) ;
446
446
} , 5000 , { trailing : false } ) ;
447
447
448
+ /**
449
+ * @param {String } socketID
450
+ * @param {String } channelName
451
+ * @param {Function } callback
452
+ */
448
453
function authenticatePusher ( socketID , channelName , callback ) {
449
454
Log . info ( '[PusherAuthorizer] Attempting to authorize Pusher' , false , { channelName} ) ;
450
455
451
- API . read ( 'AuthenticatePusher' , {
456
+ // We use makeRequestWithSideEffects here because we need to authorize to Pusher (an external service) each time a user connects to any channel.
457
+ // eslint-disable-next-line rulesdir/no-api-side-effects-method
458
+ API . makeRequestWithSideEffects ( 'AuthenticatePusher' , {
452
459
socket_id : socketID ,
453
460
channel_name : channelName ,
454
461
shouldRetry : false ,
455
462
forceNetworkRequest : true ,
463
+ } ) . then ( ( response ) => {
464
+ if ( response . jsonCode === CONST . JSON_CODE . NOT_AUTHENTICATED ) {
465
+ Log . hmmm ( '[PusherAuthorizer] Unable to authenticate Pusher because authToken is expired' ) ;
466
+ callback ( new Error ( 'Pusher failed to authenticate because authToken is expired' ) , { auth : '' } ) ;
467
+
468
+ // Attempt to refresh the authToken then reconnect to Pusher
469
+ reauthenticatePusher ( ) ;
470
+ return ;
471
+ }
472
+
473
+ if ( response . jsonCode !== CONST . JSON_CODE . SUCCESS ) {
474
+ Log . hmmm ( '[PusherAuthorizer] Unable to authenticate Pusher for reason other than expired session' ) ;
475
+ callback ( new Error ( `Pusher failed to authenticate because code: ${ response . jsonCode } message: ${ response . message } ` ) , { auth : '' } ) ;
476
+ return ;
477
+ }
478
+
479
+ Log . info (
480
+ '[PusherAuthorizer] Pusher authenticated successfully' ,
481
+ false ,
482
+ { channelName} ,
483
+ ) ;
484
+ callback ( null , response ) ;
485
+ } ) . catch ( ( error ) => {
486
+ Log . hmmm ( '[PusherAuthorizer] Unhandled error: ' , { channelName, error} ) ;
487
+ callback ( new Error ( 'Push_Authenticate request failed' ) , { auth : '' } ) ;
456
488
} ) ;
457
489
}
458
490
0 commit comments