Fix router logic to account for logged out users; closes #4085
Middleware was removed from the `/` route to redirect users without authentication, so now we need to handle this on the front-end properly.
This commit is contained in:
parent
b051718afe
commit
3fceb588fb
5 changed files with 99 additions and 61 deletions
|
@ -13,6 +13,8 @@ import tw, { GlobalStyles as TailwindGlobalStyles } from 'twin.macro';
|
||||||
import GlobalStylesheet from '@/assets/css/GlobalStylesheet';
|
import GlobalStylesheet from '@/assets/css/GlobalStylesheet';
|
||||||
import { history } from '@/components/history';
|
import { history } from '@/components/history';
|
||||||
import { setupInterceptors } from '@/api/interceptors';
|
import { setupInterceptors } from '@/api/interceptors';
|
||||||
|
import AuthenticatedRoute from '@/components/elements/AuthenticatedRoute';
|
||||||
|
import { ServerContext } from '@/state/server';
|
||||||
|
|
||||||
interface ExtendedWindow extends Window {
|
interface ExtendedWindow extends Window {
|
||||||
SiteConfiguration?: SiteSettings;
|
SiteConfiguration?: SiteSettings;
|
||||||
|
@ -60,10 +62,20 @@ const App = () => {
|
||||||
<div css={tw`mx-auto w-auto`}>
|
<div css={tw`mx-auto w-auto`}>
|
||||||
<Router history={history}>
|
<Router history={history}>
|
||||||
<Switch>
|
<Switch>
|
||||||
<Route path="/server/:id" component={ServerRouter}/>
|
<Route path={'/auth'}>
|
||||||
<Route path="/auth" component={AuthenticationRouter}/>
|
<AuthenticationRouter/>
|
||||||
<Route path="/" component={DashboardRouter}/>
|
</Route>
|
||||||
<Route path={'*'} component={NotFound}/>
|
<AuthenticatedRoute path={'/server/:id'}>
|
||||||
|
<ServerContext.Provider>
|
||||||
|
<ServerRouter/>
|
||||||
|
</ServerContext.Provider>
|
||||||
|
</AuthenticatedRoute>
|
||||||
|
<AuthenticatedRoute path={'/'}>
|
||||||
|
<DashboardRouter/>
|
||||||
|
</AuthenticatedRoute>
|
||||||
|
<Route path={'*'}>
|
||||||
|
<NotFound/>
|
||||||
|
</Route>
|
||||||
</Switch>
|
</Switch>
|
||||||
</Router>
|
</Router>
|
||||||
</div>
|
</div>
|
||||||
|
|
16
resources/scripts/components/elements/AuthenticatedRoute.tsx
Normal file
16
resources/scripts/components/elements/AuthenticatedRoute.tsx
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
import React from 'react';
|
||||||
|
import { Redirect, Route, RouteProps } from 'react-router';
|
||||||
|
import { useStoreState } from '@/state/hooks';
|
||||||
|
|
||||||
|
export default ({ children, ...props }: Omit<RouteProps, 'render'>) => {
|
||||||
|
const isAuthenticated = useStoreState(state => !!state.user.data?.uuid);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Route
|
||||||
|
{...props}
|
||||||
|
render={({ location }) => (
|
||||||
|
isAuthenticated ? children : <Redirect to={{ pathname: '/auth/login', state: { from: location } }}/>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
|
@ -1,22 +1,29 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { Route, RouteComponentProps, Switch } from 'react-router-dom';
|
import { Route, Switch, useRouteMatch } from 'react-router-dom';
|
||||||
import LoginContainer from '@/components/auth/LoginContainer';
|
import LoginContainer from '@/components/auth/LoginContainer';
|
||||||
import ForgotPasswordContainer from '@/components/auth/ForgotPasswordContainer';
|
import ForgotPasswordContainer from '@/components/auth/ForgotPasswordContainer';
|
||||||
import ResetPasswordContainer from '@/components/auth/ResetPasswordContainer';
|
import ResetPasswordContainer from '@/components/auth/ResetPasswordContainer';
|
||||||
import LoginCheckpointContainer from '@/components/auth/LoginCheckpointContainer';
|
import LoginCheckpointContainer from '@/components/auth/LoginCheckpointContainer';
|
||||||
import { NotFound } from '@/components/elements/ScreenBlock';
|
import { NotFound } from '@/components/elements/ScreenBlock';
|
||||||
|
import { useHistory, useLocation } from 'react-router';
|
||||||
|
|
||||||
export default ({ location, history, match }: RouteComponentProps) => (
|
export default () => {
|
||||||
<div className={'pt-8 xl:pt-32'}>
|
const history = useHistory();
|
||||||
<Switch location={location}>
|
const location = useLocation();
|
||||||
<Route path={`${match.path}/login`} component={LoginContainer} exact/>
|
const { path } = useRouteMatch();
|
||||||
<Route path={`${match.path}/login/checkpoint`} component={LoginCheckpointContainer}/>
|
|
||||||
<Route path={`${match.path}/password`} component={ForgotPasswordContainer} exact/>
|
return (
|
||||||
<Route path={`${match.path}/password/reset/:token`} component={ResetPasswordContainer}/>
|
<div className={'pt-8 xl:pt-32'}>
|
||||||
<Route path={`${match.path}/checkpoint`}/>
|
<Switch location={location}>
|
||||||
<Route path={'*'}>
|
<Route path={`${path}/login`} component={LoginContainer} exact/>
|
||||||
<NotFound onBack={() => history.push('/auth/login')}/>
|
<Route path={`${path}/login/checkpoint`} component={LoginCheckpointContainer}/>
|
||||||
</Route>
|
<Route path={`${path}/password`} component={ForgotPasswordContainer} exact/>
|
||||||
</Switch>
|
<Route path={`${path}/password/reset/:token`} component={ResetPasswordContainer}/>
|
||||||
</div>
|
<Route path={`${path}/checkpoint`}/>
|
||||||
);
|
<Route path={'*'}>
|
||||||
|
<NotFound onBack={() => history.push('/auth/login')}/>
|
||||||
|
</Route>
|
||||||
|
</Switch>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { NavLink, Route, RouteComponentProps, Switch } from 'react-router-dom';
|
import { NavLink, Route, Switch } from 'react-router-dom';
|
||||||
import AccountOverviewContainer from '@/components/dashboard/AccountOverviewContainer';
|
import AccountOverviewContainer from '@/components/dashboard/AccountOverviewContainer';
|
||||||
import NavigationBar from '@/components/NavigationBar';
|
import NavigationBar from '@/components/NavigationBar';
|
||||||
import DashboardContainer from '@/components/dashboard/DashboardContainer';
|
import DashboardContainer from '@/components/dashboard/DashboardContainer';
|
||||||
|
@ -8,37 +8,42 @@ import { NotFound } from '@/components/elements/ScreenBlock';
|
||||||
import TransitionRouter from '@/TransitionRouter';
|
import TransitionRouter from '@/TransitionRouter';
|
||||||
import SubNavigation from '@/components/elements/SubNavigation';
|
import SubNavigation from '@/components/elements/SubNavigation';
|
||||||
import AccountSSHContainer from '@/components/dashboard/ssh/AccountSSHContainer';
|
import AccountSSHContainer from '@/components/dashboard/ssh/AccountSSHContainer';
|
||||||
|
import { useLocation } from 'react-router';
|
||||||
|
|
||||||
export default ({ location }: RouteComponentProps) => (
|
export default () => {
|
||||||
<>
|
const location = useLocation();
|
||||||
<NavigationBar/>
|
|
||||||
{location.pathname.startsWith('/account') &&
|
return (
|
||||||
<SubNavigation>
|
<>
|
||||||
<div>
|
<NavigationBar/>
|
||||||
<NavLink to={'/account'} exact>Settings</NavLink>
|
{location.pathname.startsWith('/account') &&
|
||||||
<NavLink to={'/account/api'}>API Credentials</NavLink>
|
<SubNavigation>
|
||||||
<NavLink to={'/account/ssh'}>SSH Keys</NavLink>
|
<div>
|
||||||
</div>
|
<NavLink to={'/account'} exact>Settings</NavLink>
|
||||||
</SubNavigation>
|
<NavLink to={'/account/api'}>API Credentials</NavLink>
|
||||||
}
|
<NavLink to={'/account/ssh'}>SSH Keys</NavLink>
|
||||||
<TransitionRouter>
|
</div>
|
||||||
<Switch location={location}>
|
</SubNavigation>
|
||||||
<Route path={'/'} exact>
|
}
|
||||||
<DashboardContainer/>
|
<TransitionRouter>
|
||||||
</Route>
|
<Switch location={location}>
|
||||||
<Route path={'/account'} exact>
|
<Route path={'/'} exact>
|
||||||
<AccountOverviewContainer/>
|
<DashboardContainer/>
|
||||||
</Route>
|
</Route>
|
||||||
<Route path={'/account/api'} exact>
|
<Route path={'/account'} exact>
|
||||||
<AccountApiContainer/>
|
<AccountOverviewContainer/>
|
||||||
</Route>
|
</Route>
|
||||||
<Route path={'/account/ssh'} exact>
|
<Route path={'/account/api'} exact>
|
||||||
<AccountSSHContainer/>
|
<AccountApiContainer/>
|
||||||
</Route>
|
</Route>
|
||||||
<Route path={'*'}>
|
<Route path={'/account/ssh'} exact>
|
||||||
<NotFound/>
|
<AccountSSHContainer/>
|
||||||
</Route>
|
</Route>
|
||||||
</Switch>
|
<Route path={'*'}>
|
||||||
</TransitionRouter>
|
<NotFound/>
|
||||||
</>
|
</Route>
|
||||||
);
|
</Switch>
|
||||||
|
</TransitionRouter>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import TransferListener from '@/components/server/TransferListener';
|
import TransferListener from '@/components/server/TransferListener';
|
||||||
import React, { useEffect, useState } from 'react';
|
import React, { useEffect, useState } from 'react';
|
||||||
import { NavLink, Route, RouteComponentProps, Switch } from 'react-router-dom';
|
import { NavLink, Route, Switch, useRouteMatch } from 'react-router-dom';
|
||||||
import NavigationBar from '@/components/NavigationBar';
|
import NavigationBar from '@/components/NavigationBar';
|
||||||
import ServerConsole from '@/components/server/ServerConsole';
|
import ServerConsole from '@/components/server/ServerConsole';
|
||||||
import TransitionRouter from '@/TransitionRouter';
|
import TransitionRouter from '@/TransitionRouter';
|
||||||
|
@ -31,6 +31,7 @@ import RequireServerPermission from '@/hoc/RequireServerPermission';
|
||||||
import ServerInstallSvg from '@/assets/images/server_installing.svg';
|
import ServerInstallSvg from '@/assets/images/server_installing.svg';
|
||||||
import ServerRestoreSvg from '@/assets/images/server_restore.svg';
|
import ServerRestoreSvg from '@/assets/images/server_restore.svg';
|
||||||
import ServerErrorSvg from '@/assets/images/server_error.svg';
|
import ServerErrorSvg from '@/assets/images/server_error.svg';
|
||||||
|
import { useLocation } from 'react-router';
|
||||||
|
|
||||||
const ConflictStateRenderer = () => {
|
const ConflictStateRenderer = () => {
|
||||||
const status = ServerContext.useStoreState(state => state.server.data?.status || null);
|
const status = ServerContext.useStoreState(state => state.server.data?.status || null);
|
||||||
|
@ -59,7 +60,10 @@ const ConflictStateRenderer = () => {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const ServerRouter = ({ match, location }: RouteComponentProps<{ id: string }>) => {
|
export default () => {
|
||||||
|
const match = useRouteMatch<{ id: string }>();
|
||||||
|
const location = useLocation();
|
||||||
|
|
||||||
const rootAdmin = useStoreState(state => state.user.data!.rootAdmin);
|
const rootAdmin = useStoreState(state => state.user.data!.rootAdmin);
|
||||||
const [ error, setError ] = useState('');
|
const [ error, setError ] = useState('');
|
||||||
|
|
||||||
|
@ -194,9 +198,3 @@ const ServerRouter = ({ match, location }: RouteComponentProps<{ id: string }>)
|
||||||
</React.Fragment>
|
</React.Fragment>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default (props: RouteComponentProps<any>) => (
|
|
||||||
<ServerContext.Provider>
|
|
||||||
<ServerRouter {...props}/>
|
|
||||||
</ServerContext.Provider>
|
|
||||||
);
|
|
||||||
|
|
Loading…
Reference in a new issue