Skip to content

Commit

Permalink
Merge pull request #748 from hms-dbmi-cellenics/remove-ui-hardcodings
Browse files Browse the repository at this point in the history
[BIOMAGE-1934] Remove account specific hardcodings in the UI code
  • Loading branch information
StefanBabukov authored Jul 7, 2022
2 parents 4071e04 + 05eaedf commit b44486d
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 55 deletions.
5 changes: 5 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,9 @@ module.exports = withPlugins([
},
}],
[css],
{
publicRuntimeConfig: {
domainName: process.env.DOMAIN_NAME,
},
},
], nextConfig);
15 changes: 0 additions & 15 deletions src/__test__/utils/getAccountId.test.js

This file was deleted.

2 changes: 0 additions & 2 deletions src/components/data-management/SamplesTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ import { arrayMoveImmutable } from 'utils/array-move';

import { metadataNameToKey, metadataKeyToName, temporaryMetadataKey } from 'utils/data-management/metadataUtils';
import integrationTestConstants from 'utils/integrationTestConstants';

import 'utils/css/data-management.css';

import { ClipLoader } from 'react-spinners';
import useConditionalEffect from 'utils/customHooks/useConditionalEffect';
import { METADATA_DEFAULT_VALUE } from 'redux/reducers/experiments/initialState';
Expand Down
24 changes: 3 additions & 21 deletions src/utils/amplify-config.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
import Environment, { ssrGetCurrentEnvironment } from './environment';
import getAccountId from './getAccountId';
import getAWSRegion from './getAWSRegion';

const configure = (userPoolId, identityPoolId, userPoolClientDetails) => {
const currentEnvironment = ssrGetCurrentEnvironment();
const accountId = getAccountId(currentEnvironment);
const bucketName = `biomage-originals-${currentEnvironment}-${accountId}`;

const storageConfig = {
Storage: {
AWSS3: {
bucket: bucketName,
region: 'eu-west-1',
dangerouslyConnectToHttpEndpointForTesting: currentEnvironment === Environment.DEVELOPMENT,
identityId: identityPoolId,
customPrefix: {
public: '',
},
},
},
};
const redirectProtocol = (process.env.NODE_ENV === 'development') ? 'http:' : 'https:';
const usingProtocol = (url) => url.startsWith(redirectProtocol);
const signInRedirect = userPoolClientDetails.CallbackURLs.filter(usingProtocol)[0];
const signOutRedirect = userPoolClientDetails.LogoutURLs.filter(usingProtocol)[0];

const authConfig = {
Auth: {
region: 'eu-west-1',
region: getAWSRegion(),
identityPoolId,
userPoolId,
userPoolWebClientId: userPoolClientDetails.ClientId,
Expand All @@ -45,7 +27,7 @@ const configure = (userPoolId, identityPoolId, userPoolClientDetails) => {
};

return (
{ ...authConfig, ...storageConfig }
{ ...authConfig }
);
};

Expand Down
6 changes: 4 additions & 2 deletions src/utils/apiEndpoint.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import nextConfig from 'next/config';

const getApiEndpoint = (location) => {
try {
const url = new URL(location || window.location.href);
Expand All @@ -9,8 +11,8 @@ const getApiEndpoint = (location) => {
if (url.hostname.includes('localhost') || url.hostname.includes('127.0.0.1')) {
return 'http://localhost:3000';
}

return 'https://api.scp.biomage.net';
const domainName = nextConfig()?.publicRuntimeConfig?.domainName;
return `https://api.${domainName}`;
} catch (error) {
console.error('Failed to get API endpoint.');
console.error(error);
Expand Down
17 changes: 17 additions & 0 deletions src/utils/getAWSRegion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const getAWSRegion = () => {
// only works in ssr, where process.env is available

if (process.env.NODE_ENV === 'development') {
return 'eu-west-1';
}

const { AWS_REGION, AWS_DEFAULT_REGION } = process.env;

if (!AWS_REGION && !AWS_DEFAULT_REGION) {
throw new Error('AWS_REGION or DEFAULT_REGION must be set in the environment.');
}

return AWS_REGION || AWS_DEFAULT_REGION;
};

export default getAWSRegion;
11 changes: 0 additions & 11 deletions src/utils/getAccountId.js

This file was deleted.

8 changes: 4 additions & 4 deletions src/utils/ssr/getAuthenticationInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '@aws-sdk/client-cognito-identity-provider';
import { getDefaultRoleAssumerWithWebIdentity } from '@aws-sdk/client-sts';
import { fromTokenFile } from '@aws-sdk/credential-provider-web-identity';
import getAWSRegion from 'utils/getAWSRegion';
import configure from '../amplify-config';

const getAuthenticationInfo = async () => {
Expand All @@ -19,7 +20,6 @@ const getAuthenticationInfo = async () => {
if (global.cachedAuthenticationInfo) {
return global.cachedAuthenticationInfo;
}

let additionalClientParams = {};

if (process.env.NODE_ENV !== 'development') {
Expand All @@ -33,14 +33,14 @@ const getAuthenticationInfo = async () => {

const identityPoolClient = new CognitoIdentityClient(
{
region: 'eu-west-1',
region: getAWSRegion(),
...additionalClientParams,
},
);

const userPoolClient = new CognitoIdentityProviderClient(
{
region: 'eu-west-1',
region: getAWSRegion(),
...additionalClientParams,
},
);
Expand Down Expand Up @@ -91,7 +91,7 @@ const getAuthenticationInfo = async () => {
const amplifyConfig = configure(
userPoolId,
identityPoolId,
{ ...userPoolClientDetails, Domain: `${Domain}.auth.eu-west-1.amazoncognito.com` },
{ ...userPoolClientDetails, Domain: `${Domain}.auth.${getAWSRegion()}.amazoncognito.com` },
);

const result = {
Expand Down

0 comments on commit b44486d

Please sign in to comment.