kick back to login page if config gets 401 response

This commit is contained in:
Keith Grant
2020-09-16 15:35:02 -07:00
parent aceb8229ba
commit 6c4f9364ee

View File

@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState, useCallback } from 'react';
import { useHistory, useLocation, withRouter } from 'react-router-dom'; import { useHistory, useLocation, withRouter } from 'react-router-dom';
import { import {
Nav, Nav,
@@ -40,15 +40,16 @@ function AppContainer({ i18n, navRouteConfig = [], children }) {
const [config, setConfig] = useState({}); const [config, setConfig] = useState({});
const [configError, setConfigError] = useState(null); const [configError, setConfigError] = useState(null);
const [isAboutModalOpen, setIsAboutModalOpen] = useState(false); const [isAboutModalOpen, setIsAboutModalOpen] = useState(false);
const [isReady, setIsReady] = useState(false);
const handleAboutModalOpen = () => setIsAboutModalOpen(true); const handleAboutModalOpen = () => setIsAboutModalOpen(true);
const handleAboutModalClose = () => setIsAboutModalOpen(false); const handleAboutModalClose = () => setIsAboutModalOpen(false);
const handleConfigErrorClose = () => setConfigError(null); const handleConfigErrorClose = () => setConfigError(null);
const handleLogout = async () => { const handleLogout = useCallback(async () => {
await RootAPI.logout(); await RootAPI.logout();
history.replace('/login'); history.replace('/login');
}; }, [history]);
useEffect(() => { useEffect(() => {
const loadConfig = async () => { const loadConfig = async () => {
@@ -63,12 +64,21 @@ function AppContainer({ i18n, navRouteConfig = [], children }) {
}, },
] = await Promise.all([ConfigAPI.read(), MeAPI.read()]); ] = await Promise.all([ConfigAPI.read(), MeAPI.read()]);
setConfig({ ...data, me }); setConfig({ ...data, me });
setIsReady(true);
} catch (err) { } catch (err) {
if (err.response.status === 401) {
handleLogout();
return;
}
setConfigError(err); setConfigError(err);
} }
}; };
loadConfig(); loadConfig();
}, [config, pathname]); }, [config, pathname, handleLogout]);
if (!isReady) {
return null;
}
const header = ( const header = (
<PageHeader <PageHeader