Skip to content

Commit

Permalink
Merge pull request #22847 from Expensify/dsilva_addingSupportalLogin
Browse files Browse the repository at this point in the history
Adding Supportal Access v0
  • Loading branch information
mountiny authored Jul 14, 2023
2 parents 9c65620 + d316b09 commit 754b6ed
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import * as Environment from './libs/Environment/Environment';
import {WindowDimensionsProvider} from './components/withWindowDimensions';
import {KeyboardStateProvider} from './components/withKeyboardState';
import {CurrentReportIDContextProvider} from './components/withCurrentReportID';
import * as Session from './libs/actions/Session';

// For easier debugging and development, when we are in web we expose Onyx to the window, so you can more easily set data into Onyx
if (window && Environment.isDevelopment()) {
window.Onyx = Onyx;
window.setSupportToken = Session.setSupportAuthToken;
}

LogBox.ignoreLogs([
Expand Down
27 changes: 27 additions & 0 deletions src/libs/Network/NetworkStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ONYXKEYS from '../../ONYXKEYS';

let credentials;
let authToken;
let supportAuthToken;
let currentUserEmail;
let offline = false;
let authenticating = false;
Expand Down Expand Up @@ -53,6 +54,7 @@ Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (val) => {
authToken = lodashGet(val, 'authToken', null);
supportAuthToken = lodashGet(val, 'supportAuthToken', null);
currentUserEmail = lodashGet(val, 'email', null);
checkRequiredData();
},
Expand Down Expand Up @@ -105,6 +107,28 @@ function getAuthToken() {
return authToken;
}

/**
* @param {String} command
* @returns {[String]}
*/
function isSupportRequest(command) {
return _.contains(['OpenApp', 'ReconnectApp', 'OpenReport'], command);
}

/**
* @returns {String}
*/
function getSupportAuthToken() {
return supportAuthToken;
}

/**
* @param {String} newSupportAuthToken
*/
function setSupportAuthToken(newSupportAuthToken) {
supportAuthToken = newSupportAuthToken;
}

/**
* @param {String} newAuthToken
*/
Expand Down Expand Up @@ -152,4 +176,7 @@ export {
setIsAuthenticating,
getCredentials,
checkRequiredData,
getSupportAuthToken,
setSupportAuthToken,
isSupportRequest,
};
8 changes: 6 additions & 2 deletions src/libs/Network/enhanceParameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ function isAuthTokenRequired(command) {
export default function enhanceParameters(command, parameters) {
const finalParameters = {...parameters};

if (isAuthTokenRequired(command) && !parameters.authToken) {
finalParameters.authToken = NetworkStore.getAuthToken();
if (isAuthTokenRequired(command)) {
if (NetworkStore.getSupportAuthToken() && NetworkStore.isSupportRequest(command)) {
finalParameters.authToken = NetworkStore.getSupportAuthToken();
} else if (!parameters.authToken) {
finalParameters.authToken = NetworkStore.getAuthToken();
}
}

finalParameters.referer = CONFIG.EXPENSIFY.EXPENSIFY_CASH_REFERER;
Expand Down
9 changes: 8 additions & 1 deletion src/libs/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ let middlewares = [];
*/
function makeXHR(request) {
const finalParameters = enhanceParameters(request.command, request.data);
return NetworkStore.hasReadRequiredDataFromStorage().then(() => HttpUtils.xhr(request.command, finalParameters, request.type, request.shouldUseSecure));
return NetworkStore.hasReadRequiredDataFromStorage().then(() => {
// If we're using the Supportal token and this is not a Supportal request
// let's just return a promise that will resolve itself.
if (NetworkStore.getSupportAuthToken() && !NetworkStore.isSupportRequest(request.command)) {
return new Promise((resolve) => resolve());
}
return HttpUtils.xhr(request.command, finalParameters, request.type, request.shouldUseSecure);
});
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/libs/actions/Session/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,26 @@ function invalidateAuthToken() {
Onyx.merge(ONYXKEYS.SESSION, {authToken: 'pizza'});
}

/**
* Sets the SupportToken
* @param {String} supportToken
* @param {String} email
* @param {Number} accountID
*/
function setSupportAuthToken(supportToken, email, accountID) {
if (supportToken) {
Onyx.merge(ONYXKEYS.SESSION, {
authToken: '1',
supportAuthToken: supportToken,
email,
accountID,
});
} else {
Onyx.set(ONYXKEYS.SESSION, {});
}
NetworkStore.setSupportAuthToken(supportToken);
}

/**
* Clear the credentials and partial sign in session so the user can taken back to first Login step
*/
Expand Down Expand Up @@ -855,6 +875,7 @@ function validateTwoFactorAuth(twoFactorAuthCode) {

export {
beginSignIn,
setSupportAuthToken,
checkIfActionIsAllowed,
updatePasswordAndSignin,
signIn,
Expand Down

0 comments on commit 754b6ed

Please sign in to comment.