From 4ece481a7bade2494696427032319b09ea964d36 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Wed, 12 Apr 2023 14:02:27 -1000 Subject: [PATCH 1/3] Remove usage of Network.post(). Will look into cleaning up the makeRequestWithSideEffects next --- src/components/DeeplinkWrapper/index.website.js | 6 ++++-- src/libs/Authentication.js | 10 ---------- src/libs/actions/App.js | 13 +++++++++++++ 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/components/DeeplinkWrapper/index.website.js b/src/components/DeeplinkWrapper/index.website.js index c0f77ae4ead2..58460fff4d85 100644 --- a/src/components/DeeplinkWrapper/index.website.js +++ b/src/components/DeeplinkWrapper/index.website.js @@ -7,7 +7,7 @@ import CONST from '../../CONST'; import CONFIG from '../../CONFIG'; import * as Browser from '../../libs/Browser'; import ONYXKEYS from '../../ONYXKEYS'; -import * as Authentication from '../../libs/Authentication'; +import * as App from '../../libs/actions/App'; const propTypes = { /** Children to render. */ @@ -59,7 +59,9 @@ class DeeplinkWrapper extends PureComponent { this.openRouteInDesktopApp(expensifyDeeplinkUrl); return; } - Authentication.getShortLivedAuthToken().then((shortLivedAuthToken) => { + + // eslint-disable-next-line rulesdir/no-thenable-actions-in-views + App.getShortLivedAuthToken().then((shortLivedAuthToken) => { params.set('email', this.props.session.email); params.set('shortLivedAuthToken', `${shortLivedAuthToken}`); const expensifyDeeplinkUrl = `${CONST.DEEPLINK_BASE_URL}${expensifyUrl.host}/transition?${params.toString()}`; diff --git a/src/libs/Authentication.js b/src/libs/Authentication.js index 52575cf2f8b8..cd32c958eb25 100644 --- a/src/libs/Authentication.js +++ b/src/libs/Authentication.js @@ -105,17 +105,7 @@ function reauthenticate(command = '') { }); } -function getShortLivedAuthToken() { - return Network.post('OpenOldDotLink', {shouldRetry: false}).then((response) => { - if (response && response.shortLivedAuthToken) { - return Promise.resolve(response.shortLivedAuthToken); - } - return Promise.reject(); - }); -} - export { reauthenticate, Authenticate, - getShortLivedAuthToken, }; diff --git a/src/libs/actions/App.js b/src/libs/actions/App.js index 30c825f40f90..1ed09cf408ec 100644 --- a/src/libs/actions/App.js +++ b/src/libs/actions/App.js @@ -286,6 +286,18 @@ function openProfile() { Navigation.navigate(ROUTES.SETTINGS_PROFILE); } +function getShortLivedAuthToken() { + // eslint-disable-next-line rulesdir/no-api-side-effects-method + return API.makeRequestWithSideEffects('OpenOldDotLink') + .then((response) => { + if (response && response.shortLivedAuthToken) { + return Promise.resolve(response.shortLivedAuthToken); + } + + return Promise.reject(); + }); +} + export { setLocale, setLocaleAndNavigate, @@ -294,4 +306,5 @@ export { openProfile, openApp, reconnectApp, + getShortLivedAuthToken, }; From bd3d21684674d17fc79075fcdcc977a632a82d45 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Thu, 13 Apr 2023 13:18:05 -1000 Subject: [PATCH 2/3] add some additional comments --- .../DeeplinkWrapper/index.website.js | 31 ++++++++++++++----- src/libs/actions/App.js | 14 ++------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/components/DeeplinkWrapper/index.website.js b/src/components/DeeplinkWrapper/index.website.js index 58460fff4d85..4e974c582749 100644 --- a/src/components/DeeplinkWrapper/index.website.js +++ b/src/components/DeeplinkWrapper/index.website.js @@ -1,3 +1,4 @@ +import _ from 'underscore'; import PropTypes from 'prop-types'; import React, {PureComponent} from 'react'; import {withOnyx} from 'react-native-onyx'; @@ -28,6 +29,10 @@ const defaultProps = { session: { email: '', authToken: '', + shortLivedTokenForDeepLink: { + errors: {}, + token: '', + }, }, }; @@ -60,17 +65,29 @@ class DeeplinkWrapper extends PureComponent { return; } - // eslint-disable-next-line rulesdir/no-thenable-actions-in-views - App.getShortLivedAuthToken().then((shortLivedAuthToken) => { + // Make the request to get a short lived token so we can redirect the user + App.beginDeepLinkRedirect(); + } + + componentDidUpdate(prevProps) { + const didShortLivedTokenAppear = !prevProps.session.shortLivedTokenForDeepLink.token && this.props.session.shortLivedTokenForDeepLink.token; + const didShortLivedTokenErrorAppear = _.isEmpty(prevProps.session.shortLivedTokenForDeepLink.errors) && !_.isEmpty(this.props.session.shortLivedTokenForDeepLink.errors); + + // The BeginDeepLinkRedirect request is successful so check to see if the desktop app is installed and then deep link the user + if (didShortLivedTokenAppear) { + const expensifyUrl = new URL(CONFIG.EXPENSIFY.NEW_EXPENSIFY_URL); + const params = new URLSearchParams(); + params.set('exitTo', `${window.location.pathname}${window.location.search}${window.location.hash}`); params.set('email', this.props.session.email); - params.set('shortLivedAuthToken', `${shortLivedAuthToken}`); + params.set('shortLivedAuthToken', `${this.props.session.shortLivedTokenForDeepLink.token}`); const expensifyDeeplinkUrl = `${CONST.DEEPLINK_BASE_URL}${expensifyUrl.host}/transition?${params.toString()}`; this.openRouteInDesktopApp(expensifyDeeplinkUrl); - }).catch(() => { - // If the request is successful, we call the updateAppInstallationCheckStatus before the prompt pops up. - // If not, we only need to make sure that the state will be updated. + } + + // The BeginDeepLinkRedirect was not successful for some reason so we'll just render the app without blocking anything or redirecting + if (didShortLivedTokenErrorAppear) { this.updateAppInstallationCheckStatus(); - }); + } } updateAppInstallationCheckStatus() { diff --git a/src/libs/actions/App.js b/src/libs/actions/App.js index 1ed09cf408ec..21933931de48 100644 --- a/src/libs/actions/App.js +++ b/src/libs/actions/App.js @@ -286,16 +286,8 @@ function openProfile() { Navigation.navigate(ROUTES.SETTINGS_PROFILE); } -function getShortLivedAuthToken() { - // eslint-disable-next-line rulesdir/no-api-side-effects-method - return API.makeRequestWithSideEffects('OpenOldDotLink') - .then((response) => { - if (response && response.shortLivedAuthToken) { - return Promise.resolve(response.shortLivedAuthToken); - } - - return Promise.reject(); - }); +function beginDeepLinkRedirect() { + API.read('BeginDeepLinkRedirect'); } export { @@ -306,5 +298,5 @@ export { openProfile, openApp, reconnectApp, - getShortLivedAuthToken, + beginDeepLinkRedirect, }; From eb53f2e0e61be8e81aab9f84e395151afbe0779c Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Thu, 13 Apr 2023 13:24:15 -1000 Subject: [PATCH 3/3] Set errors if the request to BeginDeepLinkRedirect fails --- src/libs/actions/App.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/App.js b/src/libs/actions/App.js index 21933931de48..313654d9033d 100644 --- a/src/libs/actions/App.js +++ b/src/libs/actions/App.js @@ -15,6 +15,7 @@ import ROUTES from '../../ROUTES'; import * as SessionUtils from '../SessionUtils'; import getCurrentUrl from '../Navigation/currentUrl'; import * as Session from './Session'; +import DateUtils from '../DateUtils'; let currentUserAccountID; let currentUserEmail = ''; @@ -287,7 +288,19 @@ function openProfile() { } function beginDeepLinkRedirect() { - API.read('BeginDeepLinkRedirect'); + API.read('BeginDeepLinkRedirect', {}, { + failureData: [{ + onyxMethod: CONST.ONYX.METHOD.MERGE, + key: ONYXKEYS.SESSION, + value: { + shortLivedTokenForDeepLink: { + // We won't actually show this error anywhere, but it is necessary for the DeeplinkWrapper component to continue if this request fails for some reason + // eslint-disable-next-line rulesdir/prefer-localization + errors: {[DateUtils.getMicroseconds()]: ''}, + }, + }, + }], + }); } export {