Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: authentication for private graph #2309

Merged
merged 20 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions src/components/App/Splash/SpiningSphere/index.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
import Lottie from 'react-lottie'
import lottie, { AnimationItem } from 'lottie-web'
import { Flex } from '~/components/common/Flex'

import { useEffect, useRef } from 'react'
import preloadData from './preloader.json'

export const SphereAnimation = () => (
<Flex style={{ width: '167px', height: '167px', opacity: 0.5 }}>
<Lottie
height={167}
options={{
export const SphereAnimation = () => {
const lottieRef = useRef<AnimationItem | null>(null)

useEffect(() => {
const container = document.getElementById('lottie-sphere-animation')

if (container) {
lottieRef.current = lottie.loadAnimation({
container,
animationData: preloadData,
loop: true,
autoplay: true,
animationData: preloadData,
rendererSettings: {
preserveAspectRatio: 'xMidYMid slice',
},
}}
width={167}
/>
</Flex>
)
})
}

return () => {
if (lottieRef.current) {
lottieRef.current.destroy()
}
}
}, [])

return <Flex id="lottie-sphere-animation" style={{ width: '167px', height: '167px', opacity: 0.5 }} />
}
8 changes: 5 additions & 3 deletions src/components/App/Splash/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { getAboutData, getStats } from '~/network/fetchSourcesData'
import { useAppStore } from '~/stores/useAppStore'
import { useDataStore } from '~/stores/useDataStore'
import { colors, formatSplashMessage, formatStatsResponse } from '~/utils'
import { SphereAnimation } from './SpiningSphere'
import { AnimatedTextContent } from './animated'
import { Message, initialMessageData } from './constants'
import { initialMessageData, Message } from './constants'
import { SphereAnimation } from './SpiningSphere'

export const Splash = () => {
const [message, setMessage] = useState<Message>(initialMessageData)
Expand Down Expand Up @@ -61,7 +61,9 @@ export const Splash = () => {
}

return () => {
clearInterval(intervalId)
if (intervalId) {
clearInterval(intervalId)
}
}
}, [appMetaData, fetchData, isFetching, message, stats])

Expand Down
4 changes: 0 additions & 4 deletions src/components/AppContainer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { lazy, Suspense } from 'react'
import { Route, Routes } from 'react-router-dom'
import { useDataStore } from '~/stores/useDataStore'
import { E2ETests } from '~/utils'
import { AppProviders } from '../App/Providers'
import { Splash } from '../App/Splash'
import { AuthGuard } from '../Auth'

const LazyApp = lazy(() => import('../App').then(({ App }) => ({ default: App })))

export const AppContainer = () => {
const App = <LazyApp />
const { splashDataLoading } = useDataStore((s) => s)

return (
<AppProviders>
{splashDataLoading && <Splash />}
<Suspense fallback={<div>Loading...</div>}>
<AuthGuard>
<Routes>
Expand Down
20 changes: 18 additions & 2 deletions src/components/Auth/__tests__/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,17 @@ describe('Auth Component', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
sphinx.enable.mockResolvedValue({ pubkey: 'testPubkey' })
getIsAdminMock.mockResolvedValue({ data: { isAdmin: false, isPublic: false, isMember: false } })

getIsAdminMock.mockRejectedValue({
response: {
status: 401,
data: {
status: 'error',
message: 'Permission denied',
},
},
})

getSignedMessageFromRelayMock.mockResolvedValue({ message: 'testMessage', signature: 'testSignature' })

render(
Expand All @@ -157,7 +167,13 @@ describe('Auth Component', () => {
</MemoryRouter>,
)

await waitFor(() => expect(screen.getByText(message)).toBeInTheDocument())
await waitFor(() => {
expect(getIsAdminMock).toHaveBeenCalled()
})

await waitFor(() => {
expect(screen.getByText(message)).toBeInTheDocument()
})
})

test.skip('the unauthorized state is correctly set when the user lacks proper credentials', async () => {
Expand Down
19 changes: 12 additions & 7 deletions src/components/Auth/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
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 } = useUserStore((s) => s)
const { splashDataLoading } = useDataStore((s) => s)
const [renderMainPage, setRenderMainPage] = useState(false)

const [
setTrendingTopicsFeatureFlag,
Expand Down Expand Up @@ -62,12 +66,6 @@
try {
const res = await getIsAdmin()

if (!res.data.isPublic && !res.data.isAdmin && !res.data.isMember) {
setUnauthorized(true)

return
}

if (res.data) {
localStorage.setItem('admin', JSON.stringify({ isAdmin: res.data.isAdmin }))

Expand All @@ -80,8 +78,10 @@
}

setIsAuthenticated(true)
setRenderMainPage(true)
} catch (error) {
/* not an admin */
setUnauthorized(true)
}
}, [
setIsAuthenticated,
Expand All @@ -105,7 +105,7 @@

await handleAuth()
} catch (error) {
console.log(error)

Check warning on line 108 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / eslint-run

Unexpected console statement

Check warning on line 108 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / craco-build-run

Unexpected console statement

Check warning on line 108 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addContent/addTweet.cy.ts)

Unexpected console statement

Check warning on line 108 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/sourcesTable/sourcesTable.cy.ts)

Unexpected console statement

Check warning on line 108 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addContent/addSource.cy.ts)

Unexpected console statement

Check warning on line 108 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/trendingTopics/trendingTopics.cy.ts)

Unexpected console statement

Check warning on line 108 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/curationTable/curation.cy.ts)

Unexpected console statement

Check warning on line 108 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/seeLatest/latest.cy.ts)

Unexpected console statement

Check warning on line 108 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addContent/addWebpage.cy.ts)

Unexpected console statement

Check warning on line 108 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addContent/addYoutube.cy.ts)

Unexpected console statement

Check warning on line 108 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/checkEnv.cy.ts)

Unexpected console statement

Check warning on line 108 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/createGeneratedEdges/createGeneratedEdges.cy.ts)

Unexpected console statement

Check warning on line 108 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/admin/topics.cy.ts)

Unexpected console statement

Check warning on line 108 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/admin/signin.cy.ts)

Unexpected console statement

Check warning on line 108 in src/components/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (cypress/e2e/addNode/addNodeType.cy.ts)

Unexpected console statement
}
}

Expand All @@ -125,7 +125,12 @@
)
}

return <>{children}</>
return (
<>
{splashDataLoading && <Splash />}
{renderMainPage && children}
</>
)
}

const StyledText = styled(Text)`
Expand Down
6 changes: 5 additions & 1 deletion src/utils/getSignedMessage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ export async function getSignedMessageFromRelay(): Promise<{ message: string; si
.then((storedLsat: any) => {
signingPromise = null // Reset the promise after it's resolved

const response = { message, signature: storedLsat.signature }
if (!storedLsat) {
return { message: '', signature: '' }
}

const response = { message, signature: storedLsat?.signature || '' }

storeSignatureInLocalStorage({ ...response })

Expand Down
Loading