Skip to content

Commit

Permalink
Revert "Fix API: don't allow more than one auth requests to run at th…
Browse files Browse the repository at this point in the history
…e same time"

This reverts commit 24a91ef.
  • Loading branch information
kidroca committed Jan 7, 2022
1 parent a2b45ec commit 6b5a5d1
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/libs/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as Network from './Network';
import updateSessionAuthTokens from './actions/Session/updateSessionAuthTokens';
import setSessionLoadingAndError from './actions/Session/setSessionLoadingAndError';

let pendingAuthTask;
let isAuthenticating;
let credentials;
let authToken;

Expand Down Expand Up @@ -115,6 +115,17 @@ Network.registerParameterEnhancer(addDefaultValuesToParameters);
* @param {Object} originalRequest
*/
function handleExpiredAuthToken(originalRequest) {
// When the authentication process is running, and more API requests will be requeued and they will
// be performed after authentication is done.
if (isAuthenticating) {
retry(originalRequest);
return;
}

// Prevent any more requests from being processed while authentication happens
Network.pauseRequestQueue();
isAuthenticating = true;

// eslint-disable-next-line no-use-before-define
reauthenticate(originalRequest.command)
.finally(() => retryRequest(originalRequest));
Expand Down Expand Up @@ -204,7 +215,7 @@ Network.registerErrorHandler((queuedRequest, error) => {
* @param {String} [parameters.twoFactorAuthCode]
* @param {String} [parameters.email]
* @param {String} [parameters.authToken]
* @returns {Promise<{jsonCode: Number}>}
* @returns {Promise}
*/
function Authenticate(parameters) {
const commandName = 'Authenticate';
Expand All @@ -216,15 +227,7 @@ function Authenticate(parameters) {
'partnerUserSecret',
], parameters, commandName);

// Don't run more than one Authentication requests at the same time
if (pendingAuthTask) {
return pendingAuthTask;
}

// Make any other requests wait while authentication happens
Network.pauseRequestQueue();

pendingAuthTask = Network.post(commandName, {
return Network.post(commandName, {
// When authenticating for the first time, we pass useExpensifyLogin as true so we check
// for credentials for the expensify partnerID to let users Authenticate with their expensify user
// and password.
Expand Down Expand Up @@ -271,15 +274,7 @@ function Authenticate(parameters) {
}
}
return response;
})
.finally(() => {
// The authentication process is finished so the network can be unpaused to continue
// processing requests
pendingAuthTask = null;
Network.unpauseRequestQueue();
});

return pendingAuthTask;
}

/**
Expand Down Expand Up @@ -307,9 +302,18 @@ function reauthenticate(command = '') {
// new authToken
updateSessionAuthTokens(response.authToken, response.encryptedAuthToken);
authToken = response.authToken;

// The authentication process is finished so the network can be unpaused to continue
// processing requests
isAuthenticating = false;
Network.unpauseRequestQueue();
})

.catch((error) => {
// If authentication fails, then the network can be unpaused
Network.unpauseRequestQueue();
isAuthenticating = false;

// When a fetch() fails and the "API is offline" error is thrown we won't log the user out. Most likely they
// have a spotty connection and will need to try to reauthenticate when they come back online. We will
// re-throw this error so it can be handled by callers of reauthenticate().
Expand Down

0 comments on commit 6b5a5d1

Please sign in to comment.