Skip to content

Commit 775dba6

Browse files
committed
Change to use makeRequestWithSideEffects
1 parent b7d7b42 commit 775dba6

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

src/ONYXKEYS.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,4 @@ export default {
196196
FORMS: {
197197
ADD_DEBIT_CARD_FORM: 'addDebitCardForm',
198198
},
199-
200-
// Pusher token
201-
PUSHER_AUTHORIZATION: 'pusherAuthorization',
202199
};

src/libs/PusherConnectionManager.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
11
import lodashGet from 'lodash/get';
2-
import Onyx from 'react-native-onyx';
32
import * as Pusher from './Pusher/pusher';
43
import * as Session from './actions/Session';
54
import Log from './Log';
65
import CONST from '../CONST';
7-
import ONYXKEYS from '../ONYXKEYS';
8-
9-
let authorizerCallback;
10-
Onyx.connect({
11-
key: ONYXKEYS.PUSHER_AUTHORIZATION,
12-
callback: (authorizationData) => {
13-
if (!authorizerCallback) {
14-
return;
15-
}
16-
authorizerCallback(authorizationData.error, authorizationData.token);
17-
},
18-
});
196

207
function init() {
218
/**
@@ -26,8 +13,7 @@ function init() {
2613
*/
2714
Pusher.registerCustomAuthorizer(channel => ({
2815
authorize: (socketID, callback) => {
29-
authorizerCallback = callback;
30-
Session.authenticatePusher(socketID, channel.name);
16+
Session.authenticatePusher(socketID, channel.name, callback);
3117
},
3218
}));
3319

src/libs/actions/Session/index.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,14 +445,46 @@ const reauthenticatePusher = _.throttle(() => {
445445
});
446446
}, 5000, {trailing: false});
447447

448+
/**
449+
* @param {String} socketID
450+
* @param {String} channelName
451+
* @param {Function} callback
452+
*/
448453
function authenticatePusher(socketID, channelName, callback) {
449454
Log.info('[PusherAuthorizer] Attempting to authorize Pusher', false, {channelName});
450455

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', {
452459
socket_id: socketID,
453460
channel_name: channelName,
454461
shouldRetry: false,
455462
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: ''});
456488
});
457489
}
458490

0 commit comments

Comments
 (0)