-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,152 +1,152 @@ | ||
import { PropsWithChildren, useCallback, useEffect, useState } from 'react' | ||
import * as sphinx from 'sphinx-bridge' | ||
import styled from 'styled-components' | ||
import { Flex } from '~/components/common/Flex' | ||
import { Text } from '~/components/common/Text' | ||
import { isDevelopment, isE2E } from '~/constants' | ||
import { getIsAdmin } from '~/network/auth' | ||
import { useDataStore } from '~/stores/useDataStore' | ||
import { useFeatureFlagStore } from '~/stores/useFeatureFlagStore' | ||
import { useUserStore } from '~/stores/useUserStore' | ||
import { sphinxBridge } from '~/testSphinxBridge' | ||
import { updateBudget } from '~/utils' | ||
import { isAndroid, isWebView } from '~/utils/isWebView' | ||
import { Splash } from '../App/Splash' | ||
|
||
export const AuthGuard = ({ children }: PropsWithChildren) => { | ||
const [unAuthorized, setUnauthorized] = useState(false) | ||
const { setBudget, setIsAdmin, setPubKey, setIsAuthenticated, setSwarmUiUrl } = useUserStore((s) => s) | ||
const { splashDataLoading } = useDataStore((s) => s) | ||
const [renderMainPage, setRenderMainPage] = useState(false) | ||
|
||
const { | ||
setTrendingTopicsFeatureFlag, | ||
setQueuedSourcesFeatureFlag, | ||
setCustomSchemaFeatureFlag, | ||
setRealtimeGraphFeatureFlag, | ||
setChatInterfaceFeatureFlag, | ||
setFastFiltersFeatureFlag, | ||
} = useFeatureFlagStore((s) => s) | ||
|
||
const handleAuth = useCallback(async () => { | ||
localStorage.removeItem('admin') | ||
localStorage.removeItem('signature') | ||
|
||
let sphinxEnable | ||
|
||
try { | ||
if (!isE2E) { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
sphinxEnable = await sphinx.enable() | ||
} else { | ||
sphinxEnable = await sphinxBridge.enable() | ||
} | ||
|
||
sessionStorage.setItem('isSphinx', sphinxEnable ? 'true' : 'false') | ||
|
||
setPubKey(sphinxEnable?.pubkey) | ||
} catch (error) { | ||
setPubKey('') | ||
} | ||
|
||
if (isE2E || isDevelopment) { | ||
setIsAuthenticated(true) | ||
} | ||
}, [setPubKey, setIsAuthenticated]) | ||
|
||
const handleIsAdmin = useCallback(async () => { | ||
try { | ||
const res = await getIsAdmin() | ||
|
||
if (res.data) { | ||
const isAdmin = !!res.data.isAdmin | ||
|
||
localStorage.setItem('admin', JSON.stringify({ isAdmin })) | ||
|
||
if (isAdmin && res.data.swarmUiUrl) { | ||
setSwarmUiUrl(res.data.swarmUiUrl) | ||
} | ||
|
||
setIsAdmin(isAdmin) | ||
setTrendingTopicsFeatureFlag(res.data.trendingTopics) | ||
setQueuedSourcesFeatureFlag(res.data.queuedSources) | ||
setCustomSchemaFeatureFlag(res.data.customSchema) | ||
setRealtimeGraphFeatureFlag(res.data.realtimeGraph || false) | ||
setChatInterfaceFeatureFlag(res.data.chatInterface || false) | ||
setFastFiltersFeatureFlag(res.data.fastFilters || false) | ||
} | ||
|
||
setIsAuthenticated(true) | ||
setRenderMainPage(true) | ||
} catch (error) { | ||
/* not an admin */ | ||
setUnauthorized(true) | ||
} | ||
}, [ | ||
setIsAuthenticated, | ||
setIsAdmin, | ||
setTrendingTopicsFeatureFlag, | ||
setQueuedSourcesFeatureFlag, | ||
setCustomSchemaFeatureFlag, | ||
setRealtimeGraphFeatureFlag, | ||
setChatInterfaceFeatureFlag, | ||
setFastFiltersFeatureFlag, | ||
setSwarmUiUrl, | ||
]) | ||
|
||
// auth checker | ||
useEffect(() => { | ||
const init = async () => { | ||
if (isWebView() || isE2E || isAndroid()) { | ||
try { | ||
if (isAndroid()) { | ||
// eslint-disable-next-line no-promise-executor-return | ||
await new Promise((r) => setTimeout(r, 5000)) | ||
} | ||
|
||
await handleAuth() | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
} | ||
|
||
await updateBudget(setBudget) | ||
|
||
await handleIsAdmin() | ||
} | ||
|
||
init() | ||
}, [handleAuth, handleIsAdmin, setBudget]) | ||
|
||
const message = 'This is a private Graph, Contact Admin' | ||
|
||
if (unAuthorized) { | ||
return ( | ||
<StyledFlex> | ||
<StyledText>{message}</StyledText> | ||
</StyledFlex> | ||
) | ||
} | ||
|
||
return ( | ||
<> | ||
{splashDataLoading && <Splash />} | ||
{renderMainPage && children} | ||
</> | ||
) | ||
} | ||
|
||
const StyledText = styled(Text)` | ||
font-size: 5rem; | ||
font-weight: 600; | ||
font-family: 'Barlow'; | ||
text-align: center; | ||
` | ||
|
||
const StyledFlex = styled(Flex)` | ||
justify-content: center; | ||
align-items: center; | ||
width: 100%; | ||
height: 100%; | ||
` | ||
import { PropsWithChildren, useCallback, useEffect, useState } from 'react' | ||
import * as sphinx from 'sphinx-bridge' | ||
import styled from 'styled-components' | ||
import { Flex } from '~/components/common/Flex' | ||
import { Text } from '~/components/common/Text' | ||
import { isDevelopment, isE2E } from '~/constants' | ||
import { getIsAdmin } from '~/network/auth' | ||
import { useDataStore } from '~/stores/useDataStore' | ||
import { useFeatureFlagStore } from '~/stores/useFeatureFlagStore' | ||
import { useUserStore } from '~/stores/useUserStore' | ||
import { sphinxBridge } from '~/testSphinxBridge' | ||
import { updateBudget } from '~/utils' | ||
import { isAndroid, isWebView } from '~/utils/isWebView' | ||
import { Splash } from '../App/Splash' | ||
|
||
export const AuthGuard = ({ children }: PropsWithChildren) => { | ||
const [unAuthorized, setUnauthorized] = useState(false) | ||
const { setBudget, setIsAdmin, setPubKey, setIsAuthenticated, setSwarmUiUrl } = useUserStore((s) => s) | ||
const { splashDataLoading } = useDataStore((s) => s) | ||
const [renderMainPage, setRenderMainPage] = useState(false) | ||
|
||
const { | ||
setTrendingTopicsFeatureFlag, | ||
setQueuedSourcesFeatureFlag, | ||
setCustomSchemaFeatureFlag, | ||
setRealtimeGraphFeatureFlag, | ||
setChatInterfaceFeatureFlag, | ||
setFastFiltersFeatureFlag, | ||
} = useFeatureFlagStore((s) => s) | ||
|
||
const handleAuth = useCallback(async () => { | ||
localStorage.removeItem('admin') | ||
localStorage.removeItem('signature') | ||
|
||
let sphinxEnable | ||
|
||
try { | ||
if (!isE2E) { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
sphinxEnable = await sphinx.enable() | ||
} else { | ||
sphinxEnable = await sphinxBridge.enable() | ||
} | ||
|
||
sessionStorage.setItem('isSphinx', sphinxEnable ? 'true' : 'false') | ||
|
||
setPubKey(sphinxEnable?.pubkey) | ||
} catch (error) { | ||
setPubKey('') | ||
} | ||
|
||
if (isE2E || isDevelopment) { | ||
setIsAuthenticated(true) | ||
} | ||
}, [setPubKey, setIsAuthenticated]) | ||
|
||
const handleIsAdmin = useCallback(async () => { | ||
try { | ||
const res = await getIsAdmin() | ||
|
||
if (res.data) { | ||
const isAdmin = !!res.data.isAdmin | ||
|
||
localStorage.setItem('admin', JSON.stringify({ isAdmin })) | ||
|
||
if (isAdmin && res.data.swarmUiUrl) { | ||
setSwarmUiUrl(res.data.swarmUiUrl) | ||
} | ||
|
||
setIsAdmin(isAdmin) | ||
setTrendingTopicsFeatureFlag(res.data.trendingTopics) | ||
setQueuedSourcesFeatureFlag(res.data.queuedSources) | ||
setCustomSchemaFeatureFlag(res.data.customSchema) | ||
setRealtimeGraphFeatureFlag(res.data.realtimeGraph || false) | ||
setChatInterfaceFeatureFlag(res.data.chatInterface || true) | ||
setFastFiltersFeatureFlag(res.data.fastFilters || false) | ||
} | ||
|
||
setIsAuthenticated(true) | ||
setRenderMainPage(true) | ||
} catch (error) { | ||
/* not an admin */ | ||
setUnauthorized(true) | ||
} | ||
}, [ | ||
setIsAuthenticated, | ||
setIsAdmin, | ||
setTrendingTopicsFeatureFlag, | ||
setQueuedSourcesFeatureFlag, | ||
setCustomSchemaFeatureFlag, | ||
setRealtimeGraphFeatureFlag, | ||
setChatInterfaceFeatureFlag, | ||
setFastFiltersFeatureFlag, | ||
setSwarmUiUrl, | ||
]) | ||
|
||
// auth checker | ||
useEffect(() => { | ||
const init = async () => { | ||
if (isWebView() || isE2E || isAndroid()) { | ||
try { | ||
if (isAndroid()) { | ||
// eslint-disable-next-line no-promise-executor-return | ||
await new Promise((r) => setTimeout(r, 5000)) | ||
} | ||
|
||
await handleAuth() | ||
} catch (error) { | ||
console.log(error) | ||
Check warning on line 110 in src/components/Auth/index.tsx GitHub Actions / eslint-run
Check warning on line 110 in src/components/Auth/index.tsx GitHub Actions / craco-build-run
Check warning on line 110 in src/components/Auth/index.tsx GitHub Actions / cypress-run (cypress/e2e/addContent/addSource.cy.ts)
Check warning on line 110 in src/components/Auth/index.tsx GitHub Actions / cypress-run (cypress/e2e/sourcesTable/sourcesTable.cy.ts)
Check warning on line 110 in src/components/Auth/index.tsx GitHub Actions / cypress-run (cypress/e2e/curationTable/curation.cy.ts)
Check warning on line 110 in src/components/Auth/index.tsx GitHub Actions / cypress-run (cypress/e2e/admin/topics.cy.ts)
Check warning on line 110 in src/components/Auth/index.tsx GitHub Actions / cypress-run (cypress/e2e/createGeneratedEdges/createGeneratedEdges.cy.ts)
Check warning on line 110 in src/components/Auth/index.tsx GitHub Actions / cypress-run (cypress/e2e/checkEnv.cy.ts)
Check warning on line 110 in src/components/Auth/index.tsx GitHub Actions / cypress-run (cypress/e2e/admin/signin.cy.ts)
Check warning on line 110 in src/components/Auth/index.tsx GitHub Actions / cypress-run (cypress/e2e/addContent/addWebpage.cy.ts)
Check warning on line 110 in src/components/Auth/index.tsx GitHub Actions / cypress-run (cypress/e2e/addContent/addTweet.cy.ts)
Check warning on line 110 in src/components/Auth/index.tsx GitHub Actions / cypress-run (cypress/e2e/addContent/addYoutube.cy.ts)
Check warning on line 110 in src/components/Auth/index.tsx GitHub Actions / cypress-run (cypress/e2e/trendingTopics/trendingTopics.cy.ts)
Check warning on line 110 in src/components/Auth/index.tsx GitHub Actions / cypress-run (cypress/e2e/seeLatest/latest.cy.ts)
|
||
} | ||
} | ||
|
||
await updateBudget(setBudget) | ||
|
||
await handleIsAdmin() | ||
} | ||
|
||
init() | ||
}, [handleAuth, handleIsAdmin, setBudget]) | ||
|
||
const message = 'This is a private Graph, Contact Admin' | ||
|
||
if (unAuthorized) { | ||
return ( | ||
<StyledFlex> | ||
<StyledText>{message}</StyledText> | ||
</StyledFlex> | ||
) | ||
} | ||
|
||
return ( | ||
<> | ||
{splashDataLoading && <Splash />} | ||
{renderMainPage && children} | ||
</> | ||
) | ||
} | ||
|
||
const StyledText = styled(Text)` | ||
font-size: 5rem; | ||
font-weight: 600; | ||
font-family: 'Barlow'; | ||
text-align: center; | ||
` | ||
|
||
const StyledFlex = styled(Flex)` | ||
justify-content: center; | ||
align-items: center; | ||
width: 100%; | ||
height: 100%; | ||
` |