diff --git a/frontend/src/component/App.tsx b/frontend/src/component/App.tsx index 0bdfccb329..f6a048b51e 100644 --- a/frontend/src/component/App.tsx +++ b/frontend/src/component/App.tsx @@ -28,13 +28,24 @@ interface IAppProps extends RouteComponentProps { const App = ({ location, user, fetchUiBootstrap }: IAppProps) => { const { toast, setToastData } = useToast(); // because we need the userId when the component load. - const { splash, user: userFromUseUser } = useUser(); + const { splash, user: userFromUseUser, authDetails } = useUser(); const [showSplash, setShowSplash] = useState(false); + const [showLoader, setShowLoader] = useState(false); useEffect(() => { fetchUiBootstrap(); /* eslint-disable-next-line */ }, [user.authDetails?.type]); + useEffect(() => { + // Temporary duality until redux store is removed + if (!isUnauthorized() && !userFromUseUser?.id && !authDetails) { + setShowLoader(true); + return; + } + setShowLoader(false); + /* eslint-disable-next-line */ + }, [user.authDetails, userFromUseUser.id]); + useEffect(() => { if (splash?.environment === undefined) return; if (!splash?.environment && !isUnauthorized()) { @@ -92,9 +103,10 @@ const App = ({ location, user, fetchUiBootstrap }: IAppProps) => { } elseShow={
diff --git a/frontend/src/component/providers/SWRProvider/SWRProvider.tsx b/frontend/src/component/providers/SWRProvider/SWRProvider.tsx index 31d87304f1..4d3d4d9937 100644 --- a/frontend/src/component/providers/SWRProvider/SWRProvider.tsx +++ b/frontend/src/component/providers/SWRProvider/SWRProvider.tsx @@ -5,6 +5,7 @@ import { IToast } from '../../../hooks/useToast'; interface ISWRProviderProps { setToastData: (toastData: IToast) => void; + setShowLoader: React.Dispatch>; isUnauthorized: () => boolean; } @@ -12,15 +13,16 @@ const SWRProvider: React.FC = ({ children, setToastData, isUnauthorized, + setShowLoader, }) => { const { cache } = useSWRConfig(); const history = useHistory(); const handleFetchError = error => { + setShowLoader(false); if (error.status === 401) { cache.clear(); const path = location.pathname; - mutate(USER_CACHE_KEY, { ...error.info }, false); if (path === '/login') { return; diff --git a/frontend/src/component/user/Login/Login.tsx b/frontend/src/component/user/Login/Login.tsx index 29fd52f91f..c98a86508c 100644 --- a/frontend/src/component/user/Login/Login.tsx +++ b/frontend/src/component/user/Login/Login.tsx @@ -25,7 +25,6 @@ const Login = () => { }, [permissions.length]); const resetPassword = query.get('reset') === 'true'; - return (
diff --git a/frontend/src/hooks/api/getters/useUser/useUser.ts b/frontend/src/hooks/api/getters/useUser/useUser.ts index fb0095e26e..5dcd5eb1a0 100644 --- a/frontend/src/hooks/api/getters/useUser/useUser.ts +++ b/frontend/src/hooks/api/getters/useUser/useUser.ts @@ -5,6 +5,7 @@ import { IPermission } from '../../../../interfaces/user'; import handleErrorResponses from '../httpErrorResponseHandler'; export const USER_CACHE_KEY = `api/admin/user`; +const NO_AUTH_USERNAME = 'unknown'; const useUser = ( options: SWRConfiguration = { @@ -33,12 +34,20 @@ const useUser = ( setLoading(!error && !data); }, [data, error]); + let user = data?.user; + // Set a user id if no authentication is on + // to cancel the loader. + + if (data && user?.username === NO_AUTH_USERNAME) { + user = { ...user, id: 1 }; + } + return { - user: data?.user || {}, + user: user || {}, permissions: (data?.permissions || []) as IPermission[], feedback: data?.feedback || [], splash: data?.splash || {}, - authDetails: data || {}, + authDetails: data || undefined, error, loading, refetch,