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 () => {
|
||||||
|
const history = useHistory();
|
||||||
|
const location = useLocation();
|
||||||
|
const { path } = useRouteMatch();
|
||||||
|
|
||||||
|
return (
|
||||||
<div className={'pt-8 xl:pt-32'}>
|
<div className={'pt-8 xl:pt-32'}>
|
||||||
<Switch location={location}>
|
<Switch location={location}>
|
||||||
<Route path={`${match.path}/login`} component={LoginContainer} exact/>
|
<Route path={`${path}/login`} component={LoginContainer} exact/>
|
||||||
<Route path={`${match.path}/login/checkpoint`} component={LoginCheckpointContainer}/>
|
<Route path={`${path}/login/checkpoint`} component={LoginCheckpointContainer}/>
|
||||||
<Route path={`${match.path}/password`} component={ForgotPasswordContainer} exact/>
|
<Route path={`${path}/password`} component={ForgotPasswordContainer} exact/>
|
||||||
<Route path={`${match.path}/password/reset/:token`} component={ResetPasswordContainer}/>
|
<Route path={`${path}/password/reset/:token`} component={ResetPasswordContainer}/>
|
||||||
<Route path={`${match.path}/checkpoint`}/>
|
<Route path={`${path}/checkpoint`}/>
|
||||||
<Route path={'*'}>
|
<Route path={'*'}>
|
||||||
<NotFound onBack={() => history.push('/auth/login')}/>
|
<NotFound onBack={() => history.push('/auth/login')}/>
|
||||||
</Route>
|
</Route>
|
||||||
</Switch>
|
</Switch>
|
||||||
</div>
|
</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,8 +8,12 @@ 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();
|
||||||
|
|
||||||
|
return (
|
||||||
<>
|
<>
|
||||||
<NavigationBar/>
|
<NavigationBar/>
|
||||||
{location.pathname.startsWith('/account') &&
|
{location.pathname.startsWith('/account') &&
|
||||||
|
@ -42,3 +46,4 @@ export default ({ location }: RouteComponentProps) => (
|
||||||
</TransitionRouter>
|
</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