Skip to content

Commit

Permalink
Split no OICD case in another App wrapper to fix undefined context
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele committed Aug 19, 2024
1 parent 9fab2ab commit b194441
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
48 changes: 48 additions & 0 deletions frontend/src/AppWithoutOicd.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { CustomGlobalStyle } from '@components/CustomGlobalStyle'
import { FrontendErrorBoundary } from '@components/FrontendErrorBoundary'
import { GlobalStyle, THEME, ThemeProvider } from '@mtes-mct/monitor-ui'
import { LandingPage } from '@pages/LandingPage'
import { UnsupportedBrowserPage } from '@pages/UnsupportedBrowserPage'
import { isBrowserSupported } from '@utils/isBrowserSupported'
import { useCustomAuthWithoutOicd } from 'auth/hooks/useCustomAuthWithoutOicd'
import { UserAccountContext } from 'context/UserAccountContext'
import countries from 'i18n-iso-countries'
import COUNTRIES_FR from 'i18n-iso-countries/langs/fr.json'
import { RouterProvider } from 'react-router-dom'
import { CustomProvider as RsuiteCustomProvider } from 'rsuite'
import rsuiteFrFr from 'rsuite/locales/fr_FR'

import { router } from './router'

countries.registerLocale(COUNTRIES_FR)

export function AppWithoutOicd() {
const { isAuthorized, isLoading, userAccount } = useCustomAuthWithoutOicd()

if (isLoading) {
return <LandingPage />
}

if (!isAuthorized || !userAccount) {
return <LandingPage hasInsufficientRights />
}

if (!isBrowserSupported()) {
return <UnsupportedBrowserPage />
}

return (
<UserAccountContext.Provider value={userAccount}>
<ThemeProvider theme={THEME}>
<GlobalStyle />
<CustomGlobalStyle />

<RsuiteCustomProvider locale={rsuiteFrFr}>
<FrontendErrorBoundary>
<RouterProvider router={router} />
</FrontendErrorBoundary>
</RsuiteCustomProvider>
</ThemeProvider>
</UserAccountContext.Provider>
)
}
5 changes: 4 additions & 1 deletion frontend/src/api/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ export async function getCurrentUserAuthorizationFromAPI(): Promise<UserAuthoriz
* The user juste re-login, but the request did include the previous access_token found in LocalStorage,
* there is a race condition.
*/
if (authenticateResponse?.includes('authentication is required') || authenticateResponse?.includes('expired')) {
if (
!!authenticateResponse?.includes('authentication is required') ||
!!authenticateResponse?.includes('expired')
) {
return {
isLogged: false,
isSuperUser: false,
Expand Down
38 changes: 38 additions & 0 deletions frontend/src/auth/hooks/useCustomAuthWithoutOicd.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { noop } from 'lodash'
import { useEffect, useMemo, useState } from 'react'

import { getCurrentUserAuthorization } from '../../domain/use_cases/authorization/getCurrentUserAuthorization'

import type { UserAccountContextType } from '../../context/UserAccountContext'
import type { UserAuthorization } from '../../domain/entities/authorization/types'

export function useCustomAuthWithoutOicd(): {
isAuthorized: boolean
isLoading: boolean
userAccount: UserAccountContextType | undefined
} {
const [userAuthorization, setUserAuthorization] = useState<UserAuthorization | undefined>(undefined)

useEffect(() => {
setTimeout(async () => {
const nextUserAuthorization = await getCurrentUserAuthorization()

setUserAuthorization(nextUserAuthorization)
}, 250)
}, [])

const userAccount = useMemo(
() => ({
email: 'bob@see.org',
isSuperUser: userAuthorization?.isSuperUser ?? false,
logout: noop
}),
[userAuthorization]
)

if (!userAuthorization || userAuthorization?.isLogged === undefined) {
return { isAuthorized: false, isLoading: true, userAccount: undefined }
}

return { isAuthorized: true, isLoading: false, userAccount }
}
3 changes: 2 additions & 1 deletion frontend/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { browserTracingIntegration, init, replayIntegration } from '@sentry/react'
import { AppWithoutOicd } from 'AppWithoutOicd'
import { createRoot } from 'react-dom/client'
import { AuthProvider } from 'react-oidc-context'

Expand Down Expand Up @@ -39,5 +40,5 @@ if (IS_OIDC_ENABLED) {
</AuthProvider>
)
} else {
root.render(<App />)
root.render(<AppWithoutOicd />)
}

0 comments on commit b194441

Please sign in to comment.