diff --git a/packages/frontend/src/api/hooks/useAppInit.ts b/packages/frontend/src/api/hooks/useAppInit.ts index de676d36..3ab49a31 100644 --- a/packages/frontend/src/api/hooks/useAppInit.ts +++ b/packages/frontend/src/api/hooks/useAppInit.ts @@ -7,12 +7,27 @@ import { useGetFeaturesQuery, useGetSubscribedViewsQuery, } from "src/api/services"; +import * as userStore from "src/api/store/slices/user"; +import { useAppDispatch } from "../store/hooks"; +import { getRtkErrorCode } from "../utils/errorHandling"; +import { Logger } from "../utils/logging"; export const useAppInit: () => void = () => { /** * Fetch the features on app load, then we rely on redux cache to * ensure subsequent calls are cached. */ - useGetFeaturesQuery(); + const dispatch = useAppDispatch(); + const { error } = useGetFeaturesQuery(); useGetSubscribedViewsQuery(); + + if (getRtkErrorCode(error) === 401) { + /** + * If the status check or the subscribed views endpoint gives a 401 unauthorized error, + * we need to reset the auth state and reload the app + */ + Logger.debug("App::AppRoutes: 401 received"); + dispatch(userStore.resetAuthState()); + location.reload(); + } };