Skip to content

Commit

Permalink
change custom network across sub-subdomains using a subdomain restric…
Browse files Browse the repository at this point in the history
…ted temporary cookie
  • Loading branch information
radumojic committed Aug 26, 2024
1 parent df3ae65 commit 46c9b94
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/config/helpers/getInternalLinks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DEFAULT_HOSTNAME } from 'config';
import { NetworkType, NetworkUrlType } from 'types/network.types';

export const getInternalLinks = (networks: NetworkType[]): NetworkUrlType[] => {
Expand All @@ -11,7 +12,7 @@ export const getInternalLinks = (networks: NetworkType[]): NetworkUrlType[] => {
return {
id,
name,
url: `https://${id}.${process.env.VITE_APP_SHARE_PREFIX}explorer.multiversx.com`
url: `https://${id}.${process.env.VITE_APP_SHARE_PREFIX}${DEFAULT_HOSTNAME}`
};
});

Expand Down
3 changes: 3 additions & 0 deletions src/config/sharedConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export const SHARE_PREFIX = process.env.VITE_APP_SHARE_PREFIX
? process.env.VITE_APP_SHARE_PREFIX.replace('-', '')
: '';

export const DEFAULT_HOSTNAME =
process.env.VITE_APP_DEFAULT_HOSTNAME ?? 'explorer.multiversx.com';

export const hasExtraNetworks = false;

export const links: NetworkUrlType[] = [
Expand Down
7 changes: 4 additions & 3 deletions src/helpers/cookie.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { CUSTOM_NETWORK_ID } from 'appConstants';
import { DEFAULT_HOSTNAME } from 'config';

type KeyType = typeof CUSTOM_NETWORK_ID;

const domain = `domain=.${process.env.VITE_APP_SHARE_PREFIX}explorer.multiversx.com`;
const domain = `domain=.${process.env.VITE_APP_SHARE_PREFIX}${DEFAULT_HOSTNAME}`;

export const cookie = {
saveToCookies: ({
Expand All @@ -15,7 +16,7 @@ export const cookie = {
expirationDate: Date;
}) => {
const expires = `expires=${expirationDate.toUTCString()}`;
document.cookie = `${key}=${data}; ${expires}; ${domain} path=/;`;
document.cookie = `${key}=${data}; ${expires}; ${domain}; path=/;`;
},
getFromCookies: (key: KeyType) => {
const name = key + '=';
Expand All @@ -32,6 +33,6 @@ export const cookie = {
return '';
},
removeFromCookies: (key: KeyType) => {
document.cookie = `${key}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; ${domain} path=/;`;
document.cookie = `${key}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; ${domain}; path=/;`;
}
};
3 changes: 2 additions & 1 deletion src/hooks/useCustomNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ export const useCustomNetwork = (customUrl: string) => {
};

try {
const in2Minutes = new Date(moment().add(2, 'minutes').toDate());
const in30Days = new Date(moment().add(30, 'days').toDate());
const configData = {
key: CUSTOM_NETWORK_ID as typeof CUSTOM_NETWORK_ID,
data: JSON.stringify([customNetwork]),
expirationDate: in30Days
expirationDate: isSubSubdomain ? in2Minutes : in30Days
};
if (isSubSubdomain) {
cookie.saveToCookies(configData);
Expand Down
4 changes: 2 additions & 2 deletions src/redux/slices/metaTags.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit';

import { BRAND_NAME } from 'appConstants';
import { SHARE_PREFIX } from 'config';
import { DEFAULT_HOSTNAME, SHARE_PREFIX } from 'config';
import { capitalize } from 'helpers';
import { MetaTagsType } from 'types/metaTags.types';

Expand All @@ -10,7 +10,7 @@ const DEFAULT_TITLE = `${BRAND_NAME}${
} Explorer`;
const DEFAULT_DESCRIPTION =
'A highly scalable, fast and secure blockchain platform for distributed apps, enterprise use cases and the new internet economy.';
const DEFAULT_PREVIEW = `https://${process.env.VITE_APP_SHARE_PREFIX}explorer.multiversx.com/${process.env.VITE_APP_SHARE_PREFIX}share.jpg`;
const DEFAULT_PREVIEW = `https://${process.env.VITE_APP_SHARE_PREFIX}${DEFAULT_HOSTNAME}/${process.env.VITE_APP_SHARE_PREFIX}share.jpg`;

export const getInitialMetaTagsState = (): MetaTagsType => {
return {
Expand Down

0 comments on commit 46c9b94

Please sign in to comment.