diff --git a/src/libs/actions/Link.ts b/src/libs/actions/Link.ts index 702153dac0b7..19f5281e086f 100644 --- a/src/libs/actions/Link.ts +++ b/src/libs/actions/Link.ts @@ -21,9 +21,13 @@ Onyx.connect({ }); let currentUserEmail = ''; +let currentUserAccountID = -1; Onyx.connect({ key: ONYXKEYS.SESSION, - callback: (value) => (currentUserEmail = value?.email ?? ''), + callback: (value) => { + currentUserEmail = value?.email ?? ''; + currentUserAccountID = value?.accountID ?? -1; + }, }); function buildOldDotURL(url: string, shortLivedAuthToken?: string): Promise { @@ -157,4 +161,29 @@ function openLink(href: string, environmentURL: string, isAttachment = false) { openExternalLink(href); } -export {buildOldDotURL, openOldDotLink, openExternalLink, openLink, getInternalNewExpensifyPath, getInternalExpensifyPath, openTravelDotLink, buildTravelDotURL}; +function buildURLWithAuthToken(url: string, shortLivedAuthToken?: string) { + const authTokenParam = shortLivedAuthToken ? `shortLivedAuthToken=${shortLivedAuthToken}` : ''; + const emailParam = `email=${encodeURIComponent(currentUserEmail)}`; + const exitTo = `exitTo=${url}`; + const accountID = `accountID=${currentUserAccountID}`; + const paramsArray = [accountID, emailParam, authTokenParam, exitTo]; + const params = paramsArray.filter(Boolean).join('&'); + + return `${CONFIG.EXPENSIFY.NEW_EXPENSIFY_URL}transition?${params}`; +} + +/** + * @param shouldSkipCustomSafariLogic When true, we will use `Linking.openURL` even if the browser is Safari. + */ +function openExternalLinkWithToken(url: string, shouldSkipCustomSafariLogic = false) { + asyncOpenURL( + // eslint-disable-next-line rulesdir/no-api-side-effects-method + API.makeRequestWithSideEffects(SIDE_EFFECT_REQUEST_COMMANDS.OPEN_OLD_DOT_LINK, {}, {}) + .then((response) => (response ? buildURLWithAuthToken(url, response.shortLivedAuthToken) : buildURLWithAuthToken(url))) + .catch(() => buildURLWithAuthToken(url)), + (link) => link, + shouldSkipCustomSafariLogic, + ); +} + +export {buildOldDotURL, openOldDotLink, openExternalLink, openLink, getInternalNewExpensifyPath, getInternalExpensifyPath, openTravelDotLink, buildTravelDotURL, openExternalLinkWithToken}; diff --git a/src/pages/ReimbursementAccount/BankAccountStep.tsx b/src/pages/ReimbursementAccount/BankAccountStep.tsx index c9665d908b58..689b646f465a 100644 --- a/src/pages/ReimbursementAccount/BankAccountStep.tsx +++ b/src/pages/ReimbursementAccount/BankAccountStep.tsx @@ -24,7 +24,6 @@ import * as BankAccounts from '@userActions/BankAccounts'; import * as Link from '@userActions/Link'; import * as ReimbursementAccount from '@userActions/ReimbursementAccount'; import * as Session from '@userActions/Session'; -import CONFIG from '@src/CONFIG'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; @@ -86,7 +85,7 @@ function BankAccountStep({ subStep = CONST.BANK_ACCOUNT.SETUP_TYPE.PLAID; } const plaidDesktopMessage = getPlaidDesktopMessage(); - const bankAccountRoute = `${CONFIG.EXPENSIFY.NEW_EXPENSIFY_URL}${ROUTES.BANK_ACCOUNT_WITH_STEP_TO_OPEN.getRoute('new', policyID, ROUTES.WORKSPACE_INITIAL.getRoute(policyID))}`; + const bankAccountRoute = `${ROUTES.BANK_ACCOUNT_WITH_STEP_TO_OPEN.getRoute('new', policyID, ROUTES.WORKSPACE_INITIAL.getRoute(policyID))}`; const loginNames = Object.keys(loginList ?? {}); const removeExistingBankAccountDetails = () => { @@ -135,7 +134,7 @@ function BankAccountStep({ {!!plaidDesktopMessage && ( - Link.openExternalLink(bankAccountRoute)}>{translate(plaidDesktopMessage)} + Link.openExternalLinkWithToken(bankAccountRoute)}>{translate(plaidDesktopMessage)} )}