diff --git a/src/contexts/AppBarContext.tsx b/src/contexts/AppBarContext.tsx index b75e3d63..f47c5902 100644 --- a/src/contexts/AppBarContext.tsx +++ b/src/contexts/AppBarContext.tsx @@ -6,7 +6,7 @@ import { System } from "@models/systemModel"; import * as systemService from "@services/systemService"; import { useSnackbar } from "@hooks/useSnackbar"; import { axiosSource } from "@services/utils/axiosUtils"; -import { useLoggedUser } from "@hooks/useLoggedUser"; +import { useUserAuth } from "@hooks/useUserAuth"; interface AppBarTitleContextType { appBarTitle: string; @@ -43,7 +43,7 @@ export const AppBarProvider = ({ children }: AppBarTitleProviderProps) => { const location = useLocation(); const { t } = useTranslation(); const [showSnackbar] = useSnackbar(); - const [loggedUser] = useLoggedUser(); + const loggedUser = useUserAuth(); const errorMessage = t("common.defaultErrorMsg"); useEffect(() => { @@ -65,10 +65,10 @@ export const AppBarProvider = ({ children }: AppBarTitleProviderProps) => { .catch((reason) => showSnackbar(reason, errorMessage)); }; - if (loggedUser.authenticated) fetchSystems(); + if (loggedUser) fetchSystems(); return () => axiosSource.cancel("SystemsProvider - unmounting"); - }, []); + }, [useUserAuth]); const getTitleFromPathname = (pathname: string): string => { const parts = pathname.split("/"); diff --git a/src/hooks/useFailureModesTables.tsx b/src/hooks/useFailureModesTables.tsx index eebe2265..5de57830 100644 --- a/src/hooks/useFailureModesTables.tsx +++ b/src/hooks/useFailureModesTables.tsx @@ -7,7 +7,7 @@ import * as failureModesTableService from "@services/failureModesTableService"; import { SnackbarType, useSnackbar } from "./useSnackbar"; import { filter, findIndex } from "lodash"; import { FailureModesTable, UpdateFailureModesTable } from "@models/failureModesTableModel"; -import { useLoggedUser } from "./useLoggedUser"; +import { useUserAuth } from "@hooks/useUserAuth"; type failureModesTableContextType = [ FailureModesTable[], @@ -26,7 +26,7 @@ export const useFailureModesTables = () => { export const FailureModesTablesProvider = ({ children }: ChildrenProps) => { const [_tables, _setTables] = useState([]); const [showSnackbar] = useSnackbar(); - const [loggedUser] = useLoggedUser(); + const loggedUser = useUserAuth(); useEffect(() => { const fetchTables = async () => { @@ -36,7 +36,7 @@ export const FailureModesTablesProvider = ({ children }: ChildrenProps) => { .catch((reason) => showSnackbar(reason, SnackbarType.ERROR)); }; - if (loggedUser.authenticated) fetchTables(); + if (loggedUser) fetchTables(); return () => axiosSource.cancel("FailureModesTablesProvider - unmounting"); }, []); diff --git a/src/hooks/useFaultTrees.tsx b/src/hooks/useFaultTrees.tsx index 30684cb9..262f999f 100644 --- a/src/hooks/useFaultTrees.tsx +++ b/src/hooks/useFaultTrees.tsx @@ -7,7 +7,7 @@ import { axiosSource } from "@services/utils/axiosUtils"; import { ChildrenProps } from "@utils/hookUtils"; import { SnackbarType, useSnackbar } from "@hooks/useSnackbar"; import { filter, findIndex } from "lodash"; -import { useLoggedUser } from "./useLoggedUser"; +import { useUserAuth } from "@hooks/useUserAuth"; type faultTreeContextType = [ FaultTree[], @@ -26,7 +26,7 @@ export const useFaultTrees = () => { export const FaultTreesProvider = ({ children }: ChildrenProps) => { const [_faultTrees, _setFaultTrees] = useState([]); const [showSnackbar] = useSnackbar(); - const [loggedUser] = useLoggedUser(); + const loggedUser = useUserAuth(); useEffect(() => { const fetchFaultTrees = async () => { @@ -36,8 +36,7 @@ export const FaultTreesProvider = ({ children }: ChildrenProps) => { .catch((reason) => showSnackbar(reason, SnackbarType.ERROR)); }; - if (loggedUser.authenticated) fetchFaultTrees(); - + if (loggedUser) fetchFaultTrees(); return () => axiosSource.cancel("FaultTreesProvider - unmounting"); }, []); diff --git a/src/hooks/useSystems.tsx b/src/hooks/useSystems.tsx index b3ebe26d..723682e4 100644 --- a/src/hooks/useSystems.tsx +++ b/src/hooks/useSystems.tsx @@ -7,7 +7,7 @@ import { axiosSource } from "@services/utils/axiosUtils"; import { ChildrenProps } from "@utils/hookUtils"; import { SnackbarType, useSnackbar } from "@hooks/useSnackbar"; import { filter, findIndex } from "lodash"; -import { useLoggedUser } from "./useLoggedUser"; +import { useUserAuth } from "@hooks/useUserAuth"; type systemContextType = [ System[], @@ -26,7 +26,7 @@ export const useSystems = () => { export const SystemsProvider = ({ children }: ChildrenProps) => { const [_systems, _setSystems] = useState([]); const [showSnackbar] = useSnackbar(); - const [loggedUser] = useLoggedUser(); + const loggedUser = useUserAuth(); useEffect(() => { const fetchSystems = async () => { @@ -36,7 +36,7 @@ export const SystemsProvider = ({ children }: ChildrenProps) => { .catch((reason) => showSnackbar(reason, SnackbarType.ERROR)); }; - if (loggedUser.authenticated) fetchSystems(); + if (loggedUser) fetchSystems(); return () => axiosSource.cancel("SystemsProvider - unmounting"); }, []); diff --git a/src/hooks/useUserAuth.tsx b/src/hooks/useUserAuth.tsx new file mode 100644 index 00000000..0526e06a --- /dev/null +++ b/src/hooks/useUserAuth.tsx @@ -0,0 +1,14 @@ +import { useContext } from "react"; +import { AuthContext } from "@oidc/OidcAuthWrapper"; +import { useLoggedUser } from "@hooks/useLoggedUser"; +import { isUsingOidcAuth } from "@utils/OidcUtils"; + +export const useUserAuth = () => { + const oidcAuthContext = useContext(AuthContext); + const [loggedUser] = useLoggedUser(); + + if (isUsingOidcAuth()) { + return oidcAuthContext.user !== null; + } + return loggedUser.authenticated; +};