diff --git a/.env.example b/.env.example index e8bee8ff..483a9897 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,4 @@ APPWRITE_FUNCTION_API_KEY= PUBLIC_APPWRITE_HOST=http://localhost/v1 -PUBLIC_MAX_FILE_SIZE="300000000" # Must be the same as in the _APP_STORAGE_LIMIT in the Appwrite env file \ No newline at end of file +PUBLIC_MAX_FILE_SIZE="300000000" # Must be the same as in the _APP_STORAGE_LIMIT in the Appwrite env file +PUBLIC_DISABLE_REGISTRATION=true \ No newline at end of file diff --git a/src/components/auth/AuthForm.tsx b/src/components/auth/AuthForm.tsx index 750087d9..00bea916 100644 --- a/src/components/auth/AuthForm.tsx +++ b/src/components/auth/AuthForm.tsx @@ -11,9 +11,11 @@ import { import { useForm, yupResolver } from "@mantine/form"; import * as yup from "yup"; import aw from "../../utils/appwrite.util"; +import { useConfig } from "../../utils/config.util"; import toast from "../../utils/toast.util"; const AuthForm = ({ mode }: { mode: "signUp" | "signIn" }) => { + const config = useConfig(); const validationSchema = yup.object().shape({ email: yup.string().email().required(), password: yup.string().min(8).required(), @@ -39,7 +41,6 @@ const AuthForm = ({ mode }: { mode: "signUp" | "signIn" }) => { .then(() => signIn(email, password)) .catch((e) => toast.error(e.message)); }; - return ( { > {mode == "signUp" ? "Sign up" : "Welcome back"} - - {mode == "signUp" - ? "You have an account already?" - : "You don't have an account yet?"}{" "} - - {mode == "signUp" ? "Sign in" : "Sign up"} - - - + {!config.DISABLE_REGISTRATION && ( + + {mode == "signUp" + ? "You have an account already?" + : "You don't have an account yet?"}{" "} + + {mode == "signUp" ? "Sign in" : "Sign up"} + + + )}
diff --git a/src/components/navBar/NavBar.tsx b/src/components/navBar/NavBar.tsx index d4ffc288..8e5885aa 100644 --- a/src/components/navBar/NavBar.tsx +++ b/src/components/navBar/NavBar.tsx @@ -15,6 +15,7 @@ import React, { useContext, useEffect, useState } from "react"; import headerStyle from "../../styles/header.style"; import aw from "../../utils/appwrite.util"; import { IsSignedInContext } from "../../utils/auth.util"; +import { useConfig } from "../../utils/config.util"; import ToggleThemeButton from "./ToggleThemeButton"; type Link = { @@ -23,62 +24,67 @@ type Link = { action?: () => Promise; }; -const authenticatedLinks: Link[] = [ - { - link: "/upload", - label: "Upload", - }, - { - label: "Sign out", - action: async () => { - await aw.account.deleteSession("current"); - window.location.reload(); - }, - }, -]; - -const unauthenticatedLinks: Link[] = [ - { - link: "/", - label: "Home", - }, - { - link: "/auth/signUp", - label: "Sign up", - }, - { - link: "/auth/signIn", - label: "Sign in", - }, -]; - const Header = () => { const [opened, toggleOpened] = useBooleanToggle(false); const [active, setActive] = useState(); const isSignedIn = useContext(IsSignedInContext); + const config = useConfig(); const { classes, cx } = headerStyle(); + const authenticatedLinks: Link[] = [ + { + link: "/upload", + label: "Upload", + }, + { + label: "Sign out", + action: async () => { + await aw.account.deleteSession("current"); + window.location.reload(); + }, + }, + ]; + + const unauthenticatedLinks: Link[] | undefined = [ + { + link: "/", + label: "Home", + }, + { + link: "/auth/signIn", + label: "Sign in", + }, + ]; + + if (!config.DISABLE_REGISTRATION) + unauthenticatedLinks.push({ + link: "/auth/signUp", + label: "Sign up", + }); + const links = isSignedIn ? authenticatedLinks : unauthenticatedLinks; const items = links.map((link) => { - // eslint-disable-next-line react-hooks/rules-of-hooks - useEffect(() => { - if (window.location.pathname == link.link) { - setActive(link.link); - } - }); - return ( - - {link.label} - - ); + if (link) { + // eslint-disable-next-line react-hooks/rules-of-hooks + useEffect(() => { + if (window.location.pathname == link.link) { + setActive(link.link); + } + }); + return ( + + {link.label} + + ); + } }); return ( diff --git a/src/components/upload/Dropzone.tsx b/src/components/upload/Dropzone.tsx index 3e1dbdf3..27b757d9 100644 --- a/src/components/upload/Dropzone.tsx +++ b/src/components/upload/Dropzone.tsx @@ -56,7 +56,6 @@ const Dropzone = ({ const config = useConfig() const { classes } = useStyles(); const openRef = useRef<() => void>(); - return (
({}); const getInitalData = async () => { setIsLoading(true); - environmentVariables = await configUtil.getGonfig(); + const environmentVariables = await configUtil.getGonfig(); aw.setEndpoint(environmentVariables.APPWRITE_HOST); + setEnvironmentVariables(environmentVariables); setIsSignedIn(await authUtil.isSignedIn()); setIsLoading(false); }; diff --git a/src/pages/api/config.ts b/src/pages/api/config.ts index 45ed9233..7dc19889 100644 --- a/src/pages/api/config.ts +++ b/src/pages/api/config.ts @@ -2,9 +2,15 @@ import type { NextApiRequest, NextApiResponse } from "next"; const handler = async (req: NextApiRequest, res: NextApiResponse) => { let publicEnvironmentVariables: any = {}; - Object.entries(process.env).forEach(([key, value]) => { - if (key.startsWith("PUBLIC")) { + 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; } }); diff --git a/src/pages/auth/signUp.tsx b/src/pages/auth/signUp.tsx index a005fa8b..1b2c4e2b 100644 --- a/src/pages/auth/signUp.tsx +++ b/src/pages/auth/signUp.tsx @@ -3,12 +3,16 @@ import React, { useContext } from "react"; import AuthForm from "../../components/auth/AuthForm"; import Meta from "../../components/Meta"; import { IsSignedInContext } from "../../utils/auth.util"; +import { useConfig } from "../../utils/config.util"; const SignUp = () => { const isSignedIn = useContext(IsSignedInContext); + const config = useConfig(); const router = useRouter(); if (isSignedIn) { router.replace("/"); + } else if (config.DISABLE_REGISTRATION) { + router.replace("/auth/signIn"); } else { return ( <> diff --git a/src/pages/upload.tsx b/src/pages/upload.tsx index 8e1069de..11b487ec 100644 --- a/src/pages/upload.tsx +++ b/src/pages/upload.tsx @@ -76,7 +76,6 @@ const Upload = () => { } } }; - if (!isSignedIn) { router.replace("/"); } else {