diff --git a/package.json b/package.json index e1099cd..ff29453 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "postcss": "^8.4.21", "react": "18.2.0", "react-dom": "18.2.0", + "react-hook-form": "^7.43.2", "sass": "^1.58.3", "tailwindcss": "^3.2.7", "typescript": "4.9.5", diff --git a/src/pages/index.tsx b/src/pages/index.tsx new file mode 100644 index 0000000..fc32c8e --- /dev/null +++ b/src/pages/index.tsx @@ -0,0 +1,31 @@ +import {GetServerSidePropsContext, GetServerSidePropsResult, NextPage} from "next"; +import {z} from "zod"; +import {env} from "@/env/server.mjs"; + +type Props = { + authenticated: boolean; +} + +export async function getServerSideProps({query}: GetServerSidePropsContext): Promise> { + const parsedKey = z.string().safeParse(query?.key); + + if (parsedKey.success && env.API_KEY === parsedKey.data) + return { + props: { + authenticated: true + } + } + + return { + redirect: { + destination: '/login', + permanent: false + } + } +} + +const IndexPage: NextPage = ({authenticated}) => { + return
+} + +export default IndexPage; \ No newline at end of file diff --git a/src/pages/login.tsx b/src/pages/login.tsx new file mode 100644 index 0000000..76c3ff8 --- /dev/null +++ b/src/pages/login.tsx @@ -0,0 +1,55 @@ +import {NextPage} from "next"; +import {useForm} from "react-hook-form"; +import {useState} from "react"; +import {useRouter} from "next/router"; + + +const LoginPage: NextPage = () => { + type FormProps = { token: string; } + const {handleSubmit, register} = useForm(); + const [error, setError] = useState(null); + const router = useRouter(); + + async function onSubmit(data: FormProps) { + const response = await fetch(`/api/check?key=${data.token}`) + if (response.status === 200) { + setError(false); + router.push({pathname: "/", query: {"key": data.token}}).then(); + } else + setError(true); + console.log(response); + } + + return
+
+
+
+
+ +
+ +
+ {error ?

+ The token given was not valid. +

: null} +
+
+ +
+
+
+
+
+} + +export default LoginPage; \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index c71e19f..a255dd4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -801,6 +801,11 @@ react-dom@18.2.0: loose-envify "^1.1.0" scheduler "^0.23.0" +react-hook-form@^7.43.2: + version "7.43.2" + resolved "https://registry.yarnpkg.com/react-hook-form/-/react-hook-form-7.43.2.tgz#d8ff71956dc3de258dce19d4b1c7e1c6a0188e67" + integrity sha512-NvD3Oe2Y9hhqo2R4I4iJigDzSLpdMnzUpNMxlnzTbdiT7NT3BW0GxWCzEtwPudZMUPbZhNcSy1EcGAygyhDORg== + react@18.2.0: version "18.2.0" resolved "https://registry.yarnpkg.com/react/-/react-18.2.0.tgz#555bd98592883255fa00de14f1151a917b5d77d5"