mirror of
https://github.com/ansible/awx.git
synced 2026-05-11 19:37:38 -02:30
Merge pull request #8168 from keithjgrant/3321-session-management
Logout session if config returns 401 Reviewed-by: John Hill <johill@redhat.com> https://github.com/unlikelyzero
This commit is contained in:
@@ -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,17 @@ 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]);
|
||||||
|
|
||||||
const header = (
|
const header = (
|
||||||
<PageHeader
|
<PageHeader
|
||||||
@@ -109,7 +115,7 @@ function AppContainer({ i18n, navRouteConfig = [], children }) {
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Page isManagedSidebar header={header} sidebar={sidebar}>
|
<Page isManagedSidebar header={header} sidebar={sidebar}>
|
||||||
<ConfigProvider value={config}>{children}</ConfigProvider>
|
{isReady && <ConfigProvider value={config}>{children}</ConfigProvider>}
|
||||||
</Page>
|
</Page>
|
||||||
<About
|
<About
|
||||||
ansible_version={config?.ansible_version}
|
ansible_version={config?.ansible_version}
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { act } from 'react-dom/test-utils';
|
import { act } from 'react-dom/test-utils';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
mountWithContexts,
|
mountWithContexts,
|
||||||
waitForElement,
|
waitForElement,
|
||||||
} from '../../../testUtils/enzymeHelpers';
|
} from '../../../testUtils/enzymeHelpers';
|
||||||
import { ConfigAPI, MeAPI, RootAPI } from '../../api';
|
import { ConfigAPI, MeAPI, RootAPI } from '../../api';
|
||||||
|
|
||||||
import AppContainer from './AppContainer';
|
import AppContainer from './AppContainer';
|
||||||
|
|
||||||
jest.mock('../../api');
|
jest.mock('../../api');
|
||||||
@@ -58,6 +56,7 @@ describe('<AppContainer />', () => {
|
|||||||
</AppContainer>
|
</AppContainer>
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
wrapper.update();
|
||||||
|
|
||||||
// page components
|
// page components
|
||||||
expect(wrapper.length).toBe(1);
|
expect(wrapper.length).toBe(1);
|
||||||
|
|||||||
Reference in New Issue
Block a user