Skip to content

Commit

Permalink
Use new AuthenticatePusher NewDot Optimized API commands
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwenmemon committed Jul 1, 2022
1 parent a757446 commit b7d7b42
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 35 deletions.
3 changes: 3 additions & 0 deletions src/ONYXKEYS.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,7 @@ export default {
FORMS: {
ADD_DEBIT_CARD_FORM: 'addDebitCardForm',
},

// Pusher token
PUSHER_AUTHORIZATION: 'pusherAuthorization',
};
16 changes: 15 additions & 1 deletion src/libs/PusherConnectionManager.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import lodashGet from 'lodash/get';
import Onyx from 'react-native-onyx';
import * as Pusher from './Pusher/pusher';
import * as Session from './actions/Session';
import Log from './Log';
import CONST from '../CONST';
import ONYXKEYS from '../ONYXKEYS';

let authorizerCallback;
Onyx.connect({
key: ONYXKEYS.PUSHER_AUTHORIZATION,
callback: (authorizationData) => {
if (!authorizerCallback) {
return;
}
authorizerCallback(authorizationData.error, authorizationData.token);
},
});

function init() {
/**
Expand All @@ -13,7 +26,8 @@ function init() {
*/
Pusher.registerCustomAuthorizer(channel => ({
authorize: (socketID, callback) => {
Session.authenticatePusher(socketID, channel.name, callback);
authorizerCallback = callback;
Session.authenticatePusher(socketID, channel.name);
},
}));

Expand Down
37 changes: 3 additions & 34 deletions src/libs/actions/Session/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as ValidationUtils from '../../ValidationUtils';
import * as Authentication from '../../Authentication';
import * as ErrorUtils from '../../ErrorUtils';
import * as Welcome from '../Welcome';
import * as API from '../../API';

let credentials = {};
Onyx.connect({
Expand Down Expand Up @@ -444,47 +445,15 @@ const reauthenticatePusher = _.throttle(() => {
});
}, 5000, {trailing: false});

/**
* @param {String} socketID
* @param {String} channelName
* @param {Function} callback
*/
function authenticatePusher(socketID, channelName, callback) {
Log.info('[PusherAuthorizer] Attempting to authorize Pusher', false, {channelName});

DeprecatedAPI.Push_Authenticate({
API.read('AuthenticatePusher', {
socket_id: socketID,
channel_name: channelName,
shouldRetry: false,
forceNetworkRequest: true,
})
.then((response) => {
if (response.jsonCode === CONST.JSON_CODE.NOT_AUTHENTICATED) {
Log.hmmm('[PusherAuthorizer] Unable to authenticate Pusher because authToken is expired');
callback(new Error('Pusher failed to authenticate because authToken is expired'), {auth: ''});

// Attempt to refresh the authToken then reconnect to Pusher
reauthenticatePusher();
return;
}

if (response.jsonCode !== CONST.JSON_CODE.SUCCESS) {
Log.hmmm('[PusherAuthorizer] Unable to authenticate Pusher for reason other than expired session');
callback(new Error(`Pusher failed to authenticate because code: ${response.jsonCode} message: ${response.message}`), {auth: ''});
return;
}

Log.info(
'[PusherAuthorizer] Pusher authenticated successfully',
false,
{channelName},
);
callback(null, response);
})
.catch((error) => {
Log.hmmm('[PusherAuthorizer] Unhandled error: ', {channelName, error});
callback(new Error('Push_Authenticate request failed'), {auth: ''});
});
});
}

/**
Expand Down

0 comments on commit b7d7b42

Please sign in to comment.