Skip to content

Commit

Permalink
fix: authentication for private graph
Browse files Browse the repository at this point in the history
  • Loading branch information
tobi-bams committed Oct 7, 2024
1 parent 8a53946 commit bd63023
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
6 changes: 2 additions & 4 deletions src/components/AppContainer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
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)
// const { splashDataLoading } = useDataStore((s) => s)

return (
<AppProviders>
{splashDataLoading && <Splash />}
{/* {splashDataLoading && <Splash />} */}
<Suspense fallback={<div>Loading...</div>}>
<AuthGuard>
<Routes>
Expand Down
25 changes: 18 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 [unAuthorized, setUnauthorized] = useState(true)
const { setBudget, setIsAdmin, setPubKey, setIsAuthenticated } = useUserStore((s) => s)
const { splashDataLoading } = useDataStore((s) => s)
const [showSplashScreen, setShowSplashScreen] = useState(true)

const [
setTrendingTopicsFeatureFlag,
Expand Down Expand Up @@ -62,11 +66,7 @@ export const AuthGuard = ({ children }: PropsWithChildren) => {
try {
const res = await getIsAdmin()

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

return
}
setUnauthorized(false)

if (res.data) {
localStorage.setItem('admin', JSON.stringify({ isAdmin: res.data.isAdmin }))
Expand All @@ -82,6 +82,8 @@ export const AuthGuard = ({ children }: PropsWithChildren) => {
setIsAuthenticated(true)
} catch (error) {
/* not an admin */
} finally {
setShowSplashScreen(false)
}
}, [
setIsAuthenticated,
Expand Down Expand Up @@ -117,6 +119,10 @@ export const AuthGuard = ({ children }: PropsWithChildren) => {

const message = 'This is a private Graph, Contact Admin'

if (showSplashScreen) {
return <>{splashDataLoading && <Splash />}</>
}

if (unAuthorized) {
return (
<StyledFlex>
Expand All @@ -125,7 +131,12 @@ export const AuthGuard = ({ children }: PropsWithChildren) => {
)
}

return <>{children}</>
return (
<>
{splashDataLoading && <Splash />}
{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 bd63023

Please sign in to comment.