From b01a5a7de6303c2752358f970e8d71daa1a16596 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 17 Nov 2022 17:28:31 +0700 Subject: Auth component and cookie function --- src/components/WithAuth.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/components/WithAuth.js (limited to 'src/components/WithAuth.js') diff --git a/src/components/WithAuth.js b/src/components/WithAuth.js new file mode 100644 index 00000000..74518b3b --- /dev/null +++ b/src/components/WithAuth.js @@ -0,0 +1,20 @@ +import { useRouter } from "next/router"; +import { useEffect, useState } from "react"; +import { getAuth } from "../helpers/auth"; + +const WithAuth = ({ children }) => { + const router = useRouter(); + const [response, setResponse] = useState(<>); + + useEffect(() => { + if (!getAuth()) { + router.replace('/login'); + } else { + setResponse(children); + } + }, [children, router]); + + return response; +} + +export default WithAuth; \ No newline at end of file -- cgit v1.2.3