Add disable home page option

This commit is contained in:
Elias Schneider
2022-05-02 22:42:27 +02:00
parent 2cdd802422
commit c02e9c4efb
10 changed files with 46 additions and 43 deletions

View File

@@ -19,28 +19,31 @@ import authUtil, { IsSignedInContext } from "../utils/auth.util";
import configUtil, { ConfigContext } from "../utils/config.util";
import { GlobalLoadingContext } from "../utils/loading.util";
function App(props: AppProps & { colorScheme: ColorScheme }) {
const { Component, pageProps } = props;
let environmentVariables: any = {};
function App(
props: AppProps & { colorScheme: ColorScheme; environmentVariables: any }
) {
const { Component, pageProps } = props;
const [colorScheme, setColorScheme] = useState<ColorScheme>(
props.colorScheme
);
const [isLoading, setIsLoading] = useState(true);
const [isSignedIn, setIsSignedIn] = useState(false);
const [environmentVariables, setEnvironmentVariables] = useState<any>({});
if (Object.keys(props.environmentVariables).length != 0) {
environmentVariables = props.environmentVariables;
}
const getInitalData = async () => {
setIsLoading(true);
const environmentVariables = await configUtil.getGonfig();
aw.setEndpoint(environmentVariables.APPWRITE_HOST);
setEnvironmentVariables(environmentVariables);
setIsSignedIn(await authUtil.isSignedIn());
setIsLoading(false);
};
useEffect(() => {
getInitalData();
}, []);
return (
<MantineProvider withGlobalStyles withNormalizeCSS theme={globalStyle}>
<ThemeProvider colorScheme={colorScheme} setColorScheme={setColorScheme}>
@@ -70,6 +73,9 @@ function App(props: AppProps & { colorScheme: ColorScheme }) {
export default App;
App.getInitialProps = ({ ctx }: { ctx: GetServerSidePropsContext }) => ({
colorScheme: getCookie("mantine-color-scheme", ctx) || "light",
});
App.getInitialProps = ({ ctx }: { ctx: GetServerSidePropsContext }) => {
return {
environmentVariables: configUtil.getGonfig(),
colorScheme: getCookie("mantine-color-scheme", ctx) || "light",
};
};

View File

@@ -1,21 +0,0 @@
import type { NextApiRequest, NextApiResponse } from "next";
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
let publicEnvironmentVariables: any = {};
Object.entries(process.env).forEach(([key, value]: any) => {
value as string | number | boolean;
if (key.startsWith("PUBLIC") && value) {
key = key.replace("PUBLIC_", "");
if (value == "false" || value == "true") {
value = JSON.parse(value);
} else if (!isNaN(Number(value))) {
value = parseInt(value as string);
}
publicEnvironmentVariables[key] = value;
}
});
res.setHeader("cache-control", "max-age=100");
res.status(200).json(publicEnvironmentVariables);
};
export default handler;

View File

@@ -15,6 +15,7 @@ import { Check } from "tabler-icons-react";
import { IsSignedInContext } from "../utils/auth.util";
import Image from "next/image";
import Meta from "../components/Meta";
import { useConfig } from "../utils/config.util";
const useStyles = createStyles((theme) => ({
inner: {
display: "flex",
@@ -69,10 +70,13 @@ const useStyles = createStyles((theme) => ({
export default function Home() {
const isSignedIn = useContext(IsSignedInContext);
const config = useConfig();
const { classes } = useStyles();
const router = useRouter();
if (isSignedIn) {
router.replace("/upload");
} else if (config.PUBLIC_DISABLE_HOME_PAGE) {
router.replace("/auth/signIn");
} else {
return (
<>