summaryrefslogtreecommitdiff
path: root/src/components
diff options
context:
space:
mode:
authorRafi Zadanly <rafizadanly@gmail.com>2022-11-17 17:28:31 +0700
committerRafi Zadanly <rafizadanly@gmail.com>2022-11-17 17:28:31 +0700
commitb01a5a7de6303c2752358f970e8d71daa1a16596 (patch)
tree6e3be86e0669096e1657e388f900ead4011f679d /src/components
parent8f9d12c64ff25e0065439d769fe79ec416bc3df8 (diff)
Auth component and cookie function
Diffstat (limited to 'src/components')
-rw-r--r--src/components/WithAuth.js20
1 files changed, 20 insertions, 0 deletions
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