diff --git a/src/lib/Network.js b/src/lib/Network.js index 7c2ab7dc8c5f6..0c33a3e0f201f 100644 --- a/src/lib/Network.js +++ b/src/lib/Network.js @@ -200,24 +200,33 @@ function request(command, data, type = 'post') { if (!reauthenticating && responseData.jsonCode === 407 && data.doNotRetry !== true) { reauthenticating = true; return Ion.get(IONKEYS.CREDENTIALS) - .then(({login, password}) => xhr('Authenticate', { - useExpensifyLogin: false, - partnerName: CONFIG.EXPENSIFY.PARTNER_NAME, - partnerPassword: CONFIG.EXPENSIFY.PARTNER_PASSWORD, - partnerUserID: login, - partnerUserSecret: password, - twoFactorAuthCode: '' - }) - .then((response) => { - reauthenticating = false; - return setSuccessfulSignInData(response); + .then((credentials) => { + // The first time we load the app we won't have credentials to re-authenticate with + // so we send the user to the signin page + if (!credentials || !credentials.login || !credentials.password) { + return redirectToSignIn(); + } + + // If we have login credentials stored in Ion, we use them to re-authenticate + return xhr('Authenticate', { + useExpensifyLogin: false, + partnerName: CONFIG.EXPENSIFY.PARTNER_NAME, + partnerPassword: CONFIG.EXPENSIFY.PARTNER_PASSWORD, + partnerUserID: credentials.login, + partnerUserSecret: credentials.password, + twoFactorAuthCode: '' }) - .then(() => xhr(command, data, type)) - .catch(() => { - reauthenticating = false; - redirectToSignIn(); - return Promise.reject(); - })); + .then((response) => { + reauthenticating = false; + return setSuccessfulSignInData(response); + }) + .then(() => xhr(command, data, type)) + .catch(() => { + reauthenticating = false; + redirectToSignIn(); + return Promise.reject(); + }); + }); } return responseData; });