Skip to content

Commit

Permalink
Handle user auth api wait time
Browse files Browse the repository at this point in the history
  • Loading branch information
louptheron committed Oct 16, 2024
1 parent b0a743e commit 3971f30
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ context('Offline management', () => {
cy.intercept(
{
method: 'GET',
pathname: '/bff/v1/vessels/reporting',
pathname: '/bff/v1/vessels/reportings',
times: 2
},
{ statusCode: 400 }
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/auth/components/RequireAuth.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { Navigate } from 'react-router-dom'

import { LoginBackground } from './Login'
import { UserAccountContext } from '../../context/UserAccountContext'
import { paths } from '../../paths'
import { LoadingSpinnerWall } from '../../ui/LoadingSpinnerWall'
import { useGetUserAccount } from '../hooks/useGetUserAccount'

export function RequireAuth({ children, redirect = false, requireSuperUser = false }) {
const userAccount = useGetUserAccount()

if (!userAccount) {
return (
<LoginBackground>
<LoadingSpinnerWall isVesselShowed />
</LoginBackground>
)
}

const handleRedirect = (path: string, shouldRedirect: boolean) => {
if (shouldRedirect) {
return <Navigate replace to={path} />
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/auth/hooks/useGetUserAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { useGetCurrentUserAuthorizationQueryOverride } from './useGetCurrentUser

import type { UserAccountContextType } from '../../context/UserAccountContext'

const IS_CYPRESS = isCypress() || true
const IS_CYPRESS = isCypress()

/**
* When using Cypress, we stub `useAuth()`
*/
export function useGetUserAccount(): UserAccountContextType {
export function useGetUserAccount(): UserAccountContextType | undefined {
// `| undefined` because it's undefined if the OIDC is disabled which is the case for Cypress tests
const auth = useAuth() as AuthContextProps | undefined
const { trackUserId } = useTracking()
Expand All @@ -38,6 +38,10 @@ export function useGetUserAccount(): UserAccountContextType {
}, [auth])

const userAccount = useMemo(() => {
if (!user) {
return undefined
}

if (IS_CYPRESS) {
return {
email: 'dummy@cypress.test',
Expand Down

0 comments on commit 3971f30

Please sign in to comment.