diff --git a/resources/scripts/components/App.tsx b/resources/scripts/components/App.tsx index 1746fb6bf..28bbb5c6a 100644 --- a/resources/scripts/components/App.tsx +++ b/resources/scripts/components/App.tsx @@ -13,6 +13,8 @@ import tw, { GlobalStyles as TailwindGlobalStyles } from 'twin.macro'; import GlobalStylesheet from '@/assets/css/GlobalStylesheet'; import { history } from '@/components/history'; import { setupInterceptors } from '@/api/interceptors'; +import AuthenticatedRoute from '@/components/elements/AuthenticatedRoute'; +import { ServerContext } from '@/state/server'; interface ExtendedWindow extends Window { SiteConfiguration?: SiteSettings; @@ -60,10 +62,20 @@ const App = () => {
- - - - + + + + + + + + + + + + + +
diff --git a/resources/scripts/components/elements/AuthenticatedRoute.tsx b/resources/scripts/components/elements/AuthenticatedRoute.tsx new file mode 100644 index 000000000..9c5f2265e --- /dev/null +++ b/resources/scripts/components/elements/AuthenticatedRoute.tsx @@ -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) => { + const isAuthenticated = useStoreState(state => !!state.user.data?.uuid); + + return ( + ( + isAuthenticated ? children : + )} + /> + ); +}; diff --git a/resources/scripts/routers/AuthenticationRouter.tsx b/resources/scripts/routers/AuthenticationRouter.tsx index f49263108..0e3a56b61 100644 --- a/resources/scripts/routers/AuthenticationRouter.tsx +++ b/resources/scripts/routers/AuthenticationRouter.tsx @@ -1,22 +1,29 @@ 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 ForgotPasswordContainer from '@/components/auth/ForgotPasswordContainer'; import ResetPasswordContainer from '@/components/auth/ResetPasswordContainer'; import LoginCheckpointContainer from '@/components/auth/LoginCheckpointContainer'; import { NotFound } from '@/components/elements/ScreenBlock'; +import { useHistory, useLocation } from 'react-router'; -export default ({ location, history, match }: RouteComponentProps) => ( -
- - - - - - - - history.push('/auth/login')}/> - - -
-); +export default () => { + const history = useHistory(); + const location = useLocation(); + const { path } = useRouteMatch(); + + return ( +
+ + + + + + + + history.push('/auth/login')}/> + + +
+ ); +}; diff --git a/resources/scripts/routers/DashboardRouter.tsx b/resources/scripts/routers/DashboardRouter.tsx index 36bc5b40e..2d907221a 100644 --- a/resources/scripts/routers/DashboardRouter.tsx +++ b/resources/scripts/routers/DashboardRouter.tsx @@ -1,5 +1,5 @@ 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 NavigationBar from '@/components/NavigationBar'; import DashboardContainer from '@/components/dashboard/DashboardContainer'; @@ -8,37 +8,42 @@ import { NotFound } from '@/components/elements/ScreenBlock'; import TransitionRouter from '@/TransitionRouter'; import SubNavigation from '@/components/elements/SubNavigation'; import AccountSSHContainer from '@/components/dashboard/ssh/AccountSSHContainer'; +import { useLocation } from 'react-router'; -export default ({ location }: RouteComponentProps) => ( - <> - - {location.pathname.startsWith('/account') && - -
- Settings - API Credentials - SSH Keys -
-
- } - - - - - - - - - - - - - - - - - - - - -); +export default () => { + const location = useLocation(); + + return ( + <> + + {location.pathname.startsWith('/account') && + +
+ Settings + API Credentials + SSH Keys +
+
+ } + + + + + + + + + + + + + + + + + + + + + ); +}; diff --git a/resources/scripts/routers/ServerRouter.tsx b/resources/scripts/routers/ServerRouter.tsx index 434b6192a..f308b44b9 100644 --- a/resources/scripts/routers/ServerRouter.tsx +++ b/resources/scripts/routers/ServerRouter.tsx @@ -1,6 +1,6 @@ import TransferListener from '@/components/server/TransferListener'; 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 ServerConsole from '@/components/server/ServerConsole'; import TransitionRouter from '@/TransitionRouter'; @@ -31,6 +31,7 @@ import RequireServerPermission from '@/hoc/RequireServerPermission'; import ServerInstallSvg from '@/assets/images/server_installing.svg'; import ServerRestoreSvg from '@/assets/images/server_restore.svg'; import ServerErrorSvg from '@/assets/images/server_error.svg'; +import { useLocation } from 'react-router'; const ConflictStateRenderer = () => { 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 [ error, setError ] = useState(''); @@ -194,9 +198,3 @@ const ServerRouter = ({ match, location }: RouteComponentProps<{ id: string }>) ); }; - -export default (props: RouteComponentProps) => ( - - - -);