misc_pterodactyl-panel/resources/scripts/components/App.tsx
DaneEveritt 3fceb588fb
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.
2022-05-28 13:32:35 -04:00

87 lines
3.2 KiB
TypeScript

import React from 'react';
import { hot } from 'react-hot-loader/root';
import { Route, Router, Switch } from 'react-router-dom';
import { StoreProvider } from 'easy-peasy';
import { store } from '@/state';
import DashboardRouter from '@/routers/DashboardRouter';
import ServerRouter from '@/routers/ServerRouter';
import AuthenticationRouter from '@/routers/AuthenticationRouter';
import { SiteSettings } from '@/state/settings';
import ProgressBar from '@/components/elements/ProgressBar';
import { NotFound } from '@/components/elements/ScreenBlock';
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;
PterodactylUser?: {
uuid: string;
username: string;
email: string;
/* eslint-disable camelcase */
root_admin: boolean;
use_totp: boolean;
language: string;
updated_at: string;
created_at: string;
/* eslint-enable camelcase */
};
}
setupInterceptors(history);
const App = () => {
const { PterodactylUser, SiteConfiguration } = (window as ExtendedWindow);
if (PterodactylUser && !store.getState().user.data) {
store.getActions().user.setUserData({
uuid: PterodactylUser.uuid,
username: PterodactylUser.username,
email: PterodactylUser.email,
language: PterodactylUser.language,
rootAdmin: PterodactylUser.root_admin,
useTotp: PterodactylUser.use_totp,
createdAt: new Date(PterodactylUser.created_at),
updatedAt: new Date(PterodactylUser.updated_at),
});
}
if (!store.getState().settings.data) {
store.getActions().settings.setSettings(SiteConfiguration!);
}
return (
<>
<GlobalStylesheet/>
<TailwindGlobalStyles/>
<StoreProvider store={store}>
<ProgressBar/>
<div css={tw`mx-auto w-auto`}>
<Router history={history}>
<Switch>
<Route path={'/auth'}>
<AuthenticationRouter/>
</Route>
<AuthenticatedRoute path={'/server/:id'}>
<ServerContext.Provider>
<ServerRouter/>
</ServerContext.Provider>
</AuthenticatedRoute>
<AuthenticatedRoute path={'/'}>
<DashboardRouter/>
</AuthenticatedRoute>
<Route path={'*'}>
<NotFound/>
</Route>
</Switch>
</Router>
</div>
</StoreProvider>
</>
);
};
export default hot(App);