From c02e9c4efbf95c3846132111533d8cabf028a8ab Mon Sep 17 00:00:00 2001 From: Elias Schneider Date: Mon, 2 May 2022 22:42:27 +0200 Subject: [PATCH] Add disable home page option --- .env.example | 3 ++- docker-compose.yml | 3 ++- next.config.js | 3 ++- package.json | 1 - src/components/navBar/NavBar.tsx | 10 +++++----- src/pages/_app.tsx | 24 +++++++++++++++--------- src/pages/api/config.ts | 21 --------------------- src/pages/index.tsx | 4 ++++ src/utils/auth.util.ts | 2 +- src/utils/config.util.ts | 18 +++++++++++++++--- 10 files changed, 46 insertions(+), 43 deletions(-) delete mode 100644 src/pages/api/config.ts diff --git a/.env.example b/.env.example index 452c881..6387efd 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,5 @@ APPWRITE_FUNCTION_API_KEY="" PUBLIC_APPWRITE_HOST="http://localhost/v1" PUBLIC_MAX_FILE_SIZE="300000000" # Note: Must be the same as in the _APP_STORAGE_LIMIT in the Appwrite env file -PUBLIC_DISABLE_REGISTRATION="true" # Note: In the Appwrite console you have to change your user limit to 0 if false and else to 1 \ No newline at end of file +PUBLIC_DISABLE_REGISTRATION="true" # Note: In the Appwrite console you have to change your user limit to 0 if false and else to 1 +PUBLIC_DISABLE_HOME_PAGE="false" \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index b9f2b6f..561e12f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,4 +9,5 @@ services: - APPWRITE_FUNCTION_API_KEY=${APPWRITE_FUNCTION_API_KEY} - PUBLIC_APPWRITE_HOST=${PUBLIC_APPWRITE_HOST} - PUBLIC_MAX_FILE_SIZE=${PUBLIC_MAX_FILE_SIZE} - - PUBLIC_DISABLE_REGISTRATION=${PUBLIC_DISABLE_REGISTRATION} \ No newline at end of file + - PUBLIC_DISABLE_REGISTRATION=${PUBLIC_DISABLE_REGISTRATION} + - PUBLIC_DISABLE_HOME_PAGE=${PUBLIC_DISABLE_HOME_PAGE} \ No newline at end of file diff --git a/next.config.js b/next.config.js index a1b14da..e60dce6 100644 --- a/next.config.js +++ b/next.config.js @@ -5,7 +5,8 @@ const withPWA = require("next-pwa"); const nextConfig = withPWA({ reactStrictMode: true, pwa: { - dest: "public" + dest: "public", + disable: process.env.NODE_ENV == "development" }, }) diff --git a/package.json b/package.json index b476306..11af5fb 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,6 @@ "build": "next build", "start": "next start", "lint": "next lint", - "init:appwrite": "cd .setup && npm install && npx ts-node index.ts", "deploy": "docker buildx build -t stonith404/pingvin-share --platform linux/amd64,linux/arm64 --push ." }, "dependencies": { diff --git a/src/components/navBar/NavBar.tsx b/src/components/navBar/NavBar.tsx index 8e5885a..90006e4 100644 --- a/src/components/navBar/NavBar.tsx +++ b/src/components/navBar/NavBar.tsx @@ -67,11 +67,11 @@ const Header = () => { const items = links.map((link) => { if (link) { // eslint-disable-next-line react-hooks/rules-of-hooks - useEffect(() => { - if (window.location.pathname == link.link) { - setActive(link.link); - } - }); + // useEffect(() => { + // if (window.location.pathname == link.link) { + // setActive(link.link); + // } + // }); return ( ( props.colorScheme ); const [isLoading, setIsLoading] = useState(true); const [isSignedIn, setIsSignedIn] = useState(false); - const [environmentVariables, setEnvironmentVariables] = useState({}); + + 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 ( @@ -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", + }; +}; diff --git a/src/pages/api/config.ts b/src/pages/api/config.ts deleted file mode 100644 index 7dc1988..0000000 --- a/src/pages/api/config.ts +++ /dev/null @@ -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; diff --git a/src/pages/index.tsx b/src/pages/index.tsx index da0521a..d9fa800 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -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 ( <> diff --git a/src/utils/auth.util.ts b/src/utils/auth.util.ts index 595ae92..ba7595d 100644 --- a/src/utils/auth.util.ts +++ b/src/utils/auth.util.ts @@ -10,7 +10,7 @@ const isSignedIn = async () => { } }; -export const IsSignedInContext = createContext(false); +export const IsSignedInContext = createContext(true); export default { isSignedIn, diff --git a/src/utils/config.util.ts b/src/utils/config.util.ts index b3c246d..8e66000 100644 --- a/src/utils/config.util.ts +++ b/src/utils/config.util.ts @@ -1,12 +1,24 @@ -import axios from "axios"; import { createContext, useContext } from "react"; export const ConfigContext = createContext({}); export const useConfig = () => useContext(ConfigContext); -const getGonfig = async() => { - return (await axios.get("/api/config")).data; +const getGonfig = () => { + 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; + } + }); + return publicEnvironmentVariables; }; export default { getGonfig };