Skip to content

Commit

Permalink
Merge branch 'revert-remove-hint' of github.com:rushatgabhane/Expensi…
Browse files Browse the repository at this point in the history
…fy-App into revert-remove-hint
  • Loading branch information
rushatgabhane committed Mar 25, 2022
2 parents fe37758 + 65c1dc7 commit 16b4eac
Show file tree
Hide file tree
Showing 20 changed files with 457 additions and 225 deletions.
4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ android {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
multiDexEnabled rootProject.ext.multiDexEnabled
versionCode 1001014405
versionName "1.1.44-5"
versionCode 1001014600
versionName "1.1.46-0"
}
splits {
abi {
Expand Down
4 changes: 2 additions & 2 deletions ios/NewExpensify/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.1.44</string>
<string>1.1.46</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleURLTypes</key>
Expand All @@ -30,7 +30,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>1.1.44.5</string>
<string>1.1.46.0</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSApplicationQueriesSchemes</key>
Expand Down
4 changes: 2 additions & 2 deletions ios/NewExpensifyTests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.1.44</string>
<string>1.1.46</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.1.44.5</string>
<string>1.1.46.0</string>
</dict>
</plist>
6 changes: 3 additions & 3 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ PODS:
- React-Core
- react-native-flipper (0.117.0):
- React-Core
- react-native-image-picker (4.1.2):
- react-native-image-picker (4.7.3):
- React-Core
- react-native-netinfo (8.0.0):
- React-Core
Expand Down Expand Up @@ -933,7 +933,7 @@ SPEC CHECKSUMS:
react-native-config: 6502b1879f97ed5ac570a029961fc35ea606cd14
react-native-document-picker: 772d04a4bc5c35da9abe27b08ac271420ae3f9ef
react-native-flipper: cd9eabd8917104c1bbdca2621717cdca3b2addef
react-native-image-picker: f45729c43d4f854508ab25c0d0f0f711a2a8a267
react-native-image-picker: ae1202414bd5c37c00b2a701daa5b6194a06b7d9
react-native-netinfo: 0124c0695373fce63cea24aeebb97ab2d237947a
react-native-pdf: 4b5a9e4465a6a3b399e91dc4838eb44ddf716d1f
react-native-performance: 8edfa2bbc9a2af4a02f01d342118e413a95145e0
Expand Down Expand Up @@ -979,4 +979,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 1199d07d3ccd1ea23300ad9e27bc9287716223bf

COCOAPODS: 1.10.1
COCOAPODS: 1.11.2
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "new.expensify",
"version": "1.1.44-5",
"version": "1.1.46-0",
"author": "Expensify, Inc.",
"homepage": "https://new.expensify.com",
"description": "New Expensify is the next generation of Expensify: a reimagination of payments based atop a foundation of chat.",
Expand Down Expand Up @@ -84,7 +84,7 @@
"react-native-google-places-autocomplete": "git+https://github.com/Expensify/react-native-google-places-autocomplete.git#3bbd17d63e6c38d38d857b50f6037c1c0376ff06",
"react-native-haptic-feedback": "^1.13.0",
"react-native-image-pan-zoom": "^2.1.12",
"react-native-image-picker": "^4.1.2",
"react-native-image-picker": "^4.7.3",
"react-native-image-size": "^1.1.3",
"react-native-keyboard-spacer": "^0.4.1",
"react-native-modal": "^13.0.0",
Expand Down
7 changes: 4 additions & 3 deletions src/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ const CONST = {
API_OFFLINE: 'session.offlineMessageRetry',
UNKNOWN_ERROR: 'Unknown error',
REQUEST_CANCELLED: 'AbortError',
FAILED_TO_FETCH: 'Failed to fetch',
ENSURE_BUGBOT: 'ENSURE_BUGBOT',
},
NETWORK: {
METHOD: {
Expand All @@ -323,10 +325,9 @@ const CONST = {
PROCESS_REQUEST_DELAY_MS: 1000,
MAX_PENDING_TIME_MS: 10 * 1000,
},
HTTP_STATUS_CODE: {
JSON_CODE: {
SUCCESS: 200,
BAD_REQUEST: 400,
UNAUTHORIZED: 401,
NOT_AUTHENTICATED: 407,
},
NVP: {
IS_FIRST_TIME_NEW_EXPENSIFY_USER: 'isFirstTimeNewExpensifyUser',
Expand Down
1 change: 1 addition & 0 deletions src/components/AttachmentPicker/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const imagePickerOptions = {
includeBase64: false,
saveToPhotos: false,
selectionLimit: 1,
includeExtra: false,
};

/**
Expand Down
120 changes: 30 additions & 90 deletions src/libs/API.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import _ from 'underscore';
import lodashGet from 'lodash/get';
import Onyx from 'react-native-onyx';
import CONST from '../CONST';
import CONFIG from '../CONFIG';
import ONYXKEYS from '../ONYXKEYS';
import getPlaidLinkTokenParameters from './getPlaidLinkTokenParameters';
import redirectToSignIn from './actions/SignInRedirect';
import isViaExpensifyCashNative from './isViaExpensifyCashNative';
Expand All @@ -12,87 +10,10 @@ import Log from './Log';
import * as Network from './Network';
import updateSessionAuthTokens from './actions/Session/updateSessionAuthTokens';
import setSessionLoadingAndError from './actions/Session/setSessionLoadingAndError';
import * as NetworkStore from './Network/NetworkStore';
import enhanceParameters from './Network/enhanceParameters';

let isAuthenticating;
let credentials;
let authToken;
let currentUserEmail;

function checkRequiredDataAndSetNetworkReady() {
if (_.isUndefined(authToken) || _.isUndefined(credentials)) {
return;
}

Network.setIsReady(true);
}

Onyx.connect({
key: ONYXKEYS.CREDENTIALS,
callback: (val) => {
credentials = val || null;
checkRequiredDataAndSetNetworkReady();
},
});

Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (val) => {
authToken = lodashGet(val, 'authToken', null);
currentUserEmail = lodashGet(val, 'email', null);
checkRequiredDataAndSetNetworkReady();
},
});

/**
* Does this command require an authToken?
*
* @param {String} command
* @return {Boolean}
*/
function isAuthTokenRequired(command) {
return !_.contains([
'Log',
'Graphite_Timer',
'Authenticate',
'GetAccountStatus',
'SetPassword',
'User_SignUp',
'ResendValidateCode',
'ResetPassword',
'User_ReopenAccount',
'ValidateEmail',
], command);
}

/**
* Adds default values to our request data
*
* @param {String} command
* @param {Object} parameters
* @returns {Object}
*/
function addDefaultValuesToParameters(command, parameters) {
const finalParameters = {...parameters};

if (isAuthTokenRequired(command) && !parameters.authToken) {
finalParameters.authToken = authToken;
}

finalParameters.referer = CONFIG.EXPENSIFY.EXPENSIFY_CASH_REFERER;

// This application does not save its authToken in cookies like the classic Expensify app.
// Setting api_setCookie to false will ensure that the Expensify API doesn't set any cookies
// and prevents interfering with the cookie authToken that Expensify classic uses.
finalParameters.api_setCookie = false;

// Unless email is already set include current user's email in every request and the server logs
finalParameters.email = lodashGet(parameters, 'email', currentUserEmail);

return finalParameters;
}

// Tie into the network layer to add auth token to the parameters of all requests
Network.registerParameterEnhancer(addDefaultValuesToParameters);

/**
* Function used to handle expired auth tokens. It re-authenticates with the API and
Expand All @@ -118,7 +39,7 @@ function handleExpiredAuthToken(originalCommand, originalParameters, originalTyp
return reauthenticate(originalCommand)
.then(() => {
// Now that the API is authenticated, make the original request again with the new authToken
const params = addDefaultValuesToParameters(originalCommand, originalParameters);
const params = enhanceParameters(originalCommand, originalParameters);
return Network.post(originalCommand, params, originalType);
})
.catch(() => (
Expand Down Expand Up @@ -159,8 +80,10 @@ Network.registerResponseHandler((queuedRequest, response) => {
});
}

if (response.jsonCode === 407) {
// Credentials haven't been initialized. We will not be able to re-authenticates with the API
if (response.jsonCode === CONST.JSON_CODE.NOT_AUTHENTICATED) {
const credentials = NetworkStore.getCredentials();

// Credentials haven't been initialized. We will not be able to re-authenticate with the API
const unableToReauthenticate = (!credentials || !credentials.autoGeneratedLogin
|| !credentials.autoGeneratedPassword);

Expand All @@ -169,13 +92,21 @@ Network.registerResponseHandler((queuedRequest, response) => {
// of the new response created by handleExpiredAuthToken.
const shouldRetry = lodashGet(queuedRequest, 'data.shouldRetry');
if (!shouldRetry || unableToReauthenticate) {
queuedRequest.resolve(response);
// Check to see if queuedRequest has a resolve method as this could be a persisted request which had it's promise handling logic stripped
// from it when persisted to storage
if (queuedRequest.resolve) {
queuedRequest.resolve(response);
}
return;
}

handleExpiredAuthToken(queuedRequest.command, queuedRequest.data, queuedRequest.type)
.then(queuedRequest.resolve)
.catch(queuedRequest.reject);
.then(queuedRequest.resolve || (() => Promise.resolve()))
.catch(queuedRequest.reject || (() => Promise.resolve()));
return;
}

if (!queuedRequest.resolve) {
return;
}

Expand All @@ -197,8 +128,12 @@ Network.registerErrorHandler((queuedRequest, error) => {
// Set an error state and signify we are done loading
setSessionLoadingAndError(false, 'Cannot connect to server');

// Reject the queued request with an API offline error so that the original caller can handle it.
queuedRequest.reject(new Error(CONST.ERROR.API_OFFLINE));
// Reject the queued request with an API offline error so that the original caller can handle it
// Note: We are checking for the reject method as this could be a persisted request which had it's promise handling logic stripped
// from it when persisted to storage
if (queuedRequest.reject) {
queuedRequest.reject(new Error(CONST.ERROR.API_OFFLINE));
}
});

/**
Expand Down Expand Up @@ -280,6 +215,7 @@ function Authenticate(parameters) {
* @returns {Promise}
*/
function reauthenticate(command = '') {
const credentials = NetworkStore.getCredentials();
return Authenticate({
useExpensifyLogin: false,
partnerName: CONFIG.EXPENSIFY.PARTNER_NAME,
Expand All @@ -297,7 +233,11 @@ function reauthenticate(command = '') {
// Update authToken in Onyx and in our local variables so that API requests will use the
// new authToken
updateSessionAuthTokens(response.authToken, response.encryptedAuthToken);
authToken = response.authToken;

// Note: It is important to manually set the authToken that is in the store here since any requests that are hooked into
// reauthenticate .then() will immediate post and use the local authToken. Onyx updates subscribers lately so it is not
// enough to do the updateSessionAuthTokens() call above.
NetworkStore.setAuthToken(response.authToken);

// The authentication process is finished so the network can be unpaused to continue
// processing requests
Expand Down
8 changes: 7 additions & 1 deletion src/libs/HttpUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ function processHTTPRequest(url, method = 'get', body = null, canCancel = true)
method,
body,
})
.then(response => response.json());
.then((response) => {
if (!response.ok) {
throw Error(response.statusText);
}

return response.json();
});
}

/**
Expand Down
Loading

0 comments on commit 16b4eac

Please sign in to comment.