Skip to content

Commit

Permalink
Merge pull request #2309 from stakwork/fix/login-private-graph
Browse files Browse the repository at this point in the history
fix: authentication for private graph
  • Loading branch information
Rassl authored Oct 8, 2024
2 parents 8a53946 + 9f515b9 commit a114123
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 29 deletions.
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 { 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 } = useUserStore((s) => s)
const { splashDataLoading } = useDataStore((s) => s)
const [renderMainPage, setRenderMainPage] = useState(false)

const [
setTrendingTopicsFeatureFlag,
Expand Down Expand Up @@ -62,12 +66,6 @@ export const AuthGuard = ({ children }: PropsWithChildren) => {
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 @@ export const AuthGuard = ({ children }: PropsWithChildren) => {
}

setIsAuthenticated(true)
setRenderMainPage(true)
} catch (error) {
/* not an admin */
setUnauthorized(true)
}
}, [
setIsAuthenticated,
Expand Down Expand Up @@ -125,7 +125,12 @@ export const AuthGuard = ({ children }: PropsWithChildren) => {
)
}

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

0 comments on commit a114123

Please sign in to comment.