fix login/logout redirect behavior

This commit is contained in:
Keith Grant
2019-10-15 10:22:48 -07:00
parent c7d73c4583
commit 0b190c2d0d
2 changed files with 34 additions and 26 deletions

View File

@@ -1,4 +1,5 @@
import React, { Component, Fragment } from 'react'; import React, { Component, Fragment } from 'react';
import { withRouter } from 'react-router-dom';
import { global_breakpoint_md } from '@patternfly/react-tokens'; import { global_breakpoint_md } from '@patternfly/react-tokens';
import { import {
Nav, Nav,
@@ -66,8 +67,9 @@ class App extends Component {
// eslint-disable-next-line class-methods-use-this // eslint-disable-next-line class-methods-use-this
async handleLogout() { async handleLogout() {
const { history } = this.props;
await RootAPI.logout(); await RootAPI.logout();
window.location.replace('/#/login'); history.replace('/login');
} }
handleAboutOpen() { handleAboutOpen() {
@@ -193,4 +195,4 @@ class App extends Component {
} }
export { App as _App }; export { App as _App };
export default withI18n()(App); export default withI18n()(withRouter(App));

View File

@@ -43,7 +43,6 @@ export function main(render) {
const el = document.getElementById('app'); const el = document.getElementById('app');
document.title = `Ansible ${BrandName}`; document.title = `Ansible ${BrandName}`;
const defaultRedirect = () => <Redirect to="/home" />;
const removeTrailingSlash = ( const removeTrailingSlash = (
<Route <Route
exact exact
@@ -56,31 +55,38 @@ export function main(render) {
}) => <Redirect to={`${pathname.slice(0, -1)}${search}${hash}`} />} }) => <Redirect to={`${pathname.slice(0, -1)}${search}${hash}`} />}
/> />
); );
const loginRoutes = (
<Switch> const defaultRedirect = () => {
{removeTrailingSlash} if (isAuthenticated(document.cookie)) {
<Route return <Redirect to="/home" />;
path="/login" }
render={() => <Login isAuthenticated={isAuthenticated} />} return (
/> <Switch>
<Redirect to="/login" /> {removeTrailingSlash}
</Switch> <Route
); path="/login"
render={() => <Login isAuthenticated={isAuthenticated} />}
/>
<Redirect to="/login" />
</Switch>
);
};
return render( return render(
<RootProvider> <RootProvider>
<I18n> <I18n>
{({ i18n }) => ( {({ i18n }) => (
<Background> <Background>
{!isAuthenticated(document.cookie) ? ( <Switch>
loginRoutes {removeTrailingSlash}
) : ( <Route path="/login" render={defaultRedirect} />
<Switch> <Route exact path="/" render={defaultRedirect} />
{removeTrailingSlash} <Route
<Route path="/login" render={defaultRedirect} /> render={() => {
<Route exact path="/" render={defaultRedirect} /> if (!isAuthenticated(document.cookie)) {
<Route return <Redirect to="/login" />;
render={() => ( }
return (
<App <App
navLabel={i18n._(t`Primary Navigation`)} navLabel={i18n._(t`Primary Navigation`)}
routeGroups={[ routeGroups={[
@@ -250,10 +256,10 @@ export function main(render) {
return <Switch>{routeList}</Switch>; return <Switch>{routeList}</Switch>;
}} }}
/> />
)} );
/> }}
</Switch> />
)} </Switch>
</Background> </Background>
)} )}
</I18n> </I18n>