diff --git a/src/libs/API.js b/src/libs/API.js index 5a3918173bbf..07de975c2405 100644 --- a/src/libs/API.js +++ b/src/libs/API.js @@ -658,6 +658,21 @@ function User_GetBetas() { return Network.post('User_GetBetas'); } +/** + * @param {Object} parameters + * @param {String} parameters.email + * @param {Boolean} [parameters.requireCertainty] + * @returns {Promise} + */ +function User_IsFromPublicDomain(parameters) { + const commandName = 'User_IsFromPublicDomain'; + requireParameters(['email'], parameters, commandName); + return Network.post(commandName, { + ...{requireCertainty: true}, + ...parameters, + }); +} + /** * @param {Object} parameters * @param {String} parameters.email @@ -880,6 +895,7 @@ export { UpdateAccount, User_SignUp, User_GetBetas, + User_IsFromPublicDomain, User_ReopenAccount, User_SecondaryLogin_Send, User_UploadAvatar, diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index a032193f97f8..5314b8b5dec7 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.js +++ b/src/libs/Navigation/AppNavigator/AuthScreens.js @@ -120,6 +120,7 @@ class AuthScreens extends React.Component { PersonalDetails.fetchPersonalDetails(); User.getUserDetails(); User.getBetas(); + User.getPublicDomainInfo(); PersonalDetails.fetchCurrencyPreferences(); fetchAllReports(true, true, true); fetchCountryCodeByRequestIP(); diff --git a/src/libs/actions/User.js b/src/libs/actions/User.js index b7e7bdaf1b32..c43701d37c30 100644 --- a/src/libs/actions/User.js +++ b/src/libs/actions/User.js @@ -1,6 +1,8 @@ import _ from 'underscore'; import lodashGet from 'lodash/get'; import Onyx from 'react-native-onyx'; +import Str from 'expensify-common/lib/str'; +import {PUBLIC_DOMAINS as COMMON_PUBLIC_DOMAINS} from 'expensify-common/lib/CONST'; import ONYXKEYS from '../../ONYXKEYS'; import * as API from '../API'; import CONST from '../../CONST'; @@ -8,9 +10,13 @@ import Navigation from '../Navigation/Navigation'; import ROUTES from '../../ROUTES'; let sessionAuthToken = ''; +let sessionEmail = ''; Onyx.connect({ key: ONYXKEYS.SESSION, - callback: val => sessionAuthToken = val ? val.authToken : '', + callback: (val) => { + sessionAuthToken = lodashGet(val, 'authToken', ''); + sessionEmail = lodashGet(val, 'email', ''); + }, }); let currentlyViewedReportID = ''; @@ -168,6 +174,38 @@ function validateLogin(accountID, validateCode) { }); } +/** + * Fetch the public domain info for the current user. + * + * This API is a bit weird in that it sometimes depends on information being cached in bedrock. + * If the info for the domain is not in bedrock, then it creates an asynchronous bedrock job to gather domain info. + * If that happens, this function will automatically retry itself in 10 minutes. + */ +function getPublicDomainInfo() { + // If this command fails, we'll retry again in 10 minutes, + // arbitrarily chosen giving Bedrock time to resolve the ClearbitCheckPublicEmail job for this email. + const RETRY_TIMEOUT = 600000; + + // First check list of common public domains + if (_.contains(COMMON_PUBLIC_DOMAINS, sessionEmail)) { + Onyx.merge(ONYXKEYS.USER, {isFromPublicDomain: true}); + return; + } + + // If it is not a common public domain, check the API + API.User_IsFromPublicDomain({email: sessionEmail}) + .then((response) => { + if (response.jsonCode === 200) { + const {isFromPublicDomain} = response; + Onyx.merge(ONYXKEYS.USER, {isFromPublicDomain}); + } else { + // eslint-disable-next-line max-len + console.debug(`Command User_IsFromPublicDomain returned error code: ${response.jsonCode}. Most likely, this means that the domain ${Str.extractEmail(sessionEmail)} is not in the bedrock cache. Retrying in ${RETRY_TIMEOUT / 1000 / 60} minutes`); + setTimeout(getPublicDomainInfo, RETRY_TIMEOUT); + } + }); +} + export { changePassword, getBetas, @@ -176,4 +214,5 @@ export { setExpensifyNewsStatus, setSecondaryLogin, validateLogin, + getPublicDomainInfo, };