Skip to content

Commit

Permalink
feat: redirect when access from forbidden country is detected (#1209)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterslany authored May 19, 2023
1 parent 0bd5424 commit 182fba6
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 20 deletions.
14 changes: 14 additions & 0 deletions src/components/Geoblock/Geoblock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React, { ReactNode } from 'react';

import { useGeoblocking } from '@/utils/hooks/use-geoblocking';

type Props = {
children: ReactNode;
};

const GeoblockingWrapper = ({ children }: Props): JSX.Element => {
useGeoblocking();
return <>{children}</>;
};

export { GeoblockingWrapper };
5 changes: 5 additions & 0 deletions src/config/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ const INTERLAY_DOS_AND_DONTS_DOCS_LINK = 'https://docs.interlay.io/#/vault/insta

const BANXA_LINK = 'http://talisman.banxa.com/';

const GEOBLOCK_API_ENDPOINT = '/check_access';
const GEOBLOCK_REDIRECTION_LINK = 'https://www.interlay.io/geoblock';

export {
BANXA_LINK,
GEOBLOCK_API_ENDPOINT,
GEOBLOCK_REDIRECTION_LINK,
INTERLAY_COMPANY_LINK,
INTERLAY_CROWDLOAN_LINK,
INTERLAY_DISCORD_LINK,
Expand Down
43 changes: 23 additions & 20 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ThemeWrapper from '@/parts/ThemeWrapper';
import { Subscriptions } from '@/utils/hooks/api/tokens/use-balances-subscription';

import App from './App';
import { GeoblockingWrapper } from './components/Geoblock/Geoblock';
import reportWebVitals from './reportWebVitals';
import { store } from './store';

Expand All @@ -30,26 +31,28 @@ const queryClient = new QueryClient();
// MEMO: temporarily removed React.StrictMode. We should add back when react-spectrum handles
// it across their library. (Issue: https://github.com/adobe/react-spectrum/issues/779#issuecomment-1353734729)
ReactDOM.render(
<Router>
<QueryClientProvider client={queryClient}>
<HelmetProvider>
<Provider store={store}>
<SubstrateProvider>
<ThemeWrapper>
<SubstrateLoadingAndErrorHandlingWrapper>
<Subscriptions>
<OverlayProvider>
<App />
</OverlayProvider>
</Subscriptions>
</SubstrateLoadingAndErrorHandlingWrapper>
</ThemeWrapper>
</SubstrateProvider>
</Provider>
</HelmetProvider>
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
</Router>,
<GeoblockingWrapper>
<Router>
<QueryClientProvider client={queryClient}>
<HelmetProvider>
<Provider store={store}>
<SubstrateProvider>
<ThemeWrapper>
<SubstrateLoadingAndErrorHandlingWrapper>
<Subscriptions>
<OverlayProvider>
<App />
</OverlayProvider>
</Subscriptions>
</SubstrateLoadingAndErrorHandlingWrapper>
</ThemeWrapper>
</SubstrateProvider>
</Provider>
</HelmetProvider>
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
</Router>
</GeoblockingWrapper>,
document.getElementById('root')
);

Expand Down
22 changes: 22 additions & 0 deletions src/utils/hooks/use-geoblocking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useEffect } from 'react';

import { GEOBLOCK_API_ENDPOINT, GEOBLOCK_REDIRECTION_LINK } from '@/config/links';

const useGeoblocking = (): void => {
useEffect(() => {
const checkCountry = async () => {
try {
const response = await fetch(GEOBLOCK_API_ENDPOINT);
if (response.status === 403) {
console.log('Access from forbidden country detected, user will be redirected.');
window.location.replace(GEOBLOCK_REDIRECTION_LINK);
}
} catch (error) {
console.log(error);
}
};
checkCountry();
}, []);
};

export { useGeoblocking };

2 comments on commit 182fba6

@vercel
Copy link

@vercel vercel bot commented on 182fba6 May 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 182fba6 May 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.