summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/components/WithAuth.js20
-rw-r--r--src/helpers/auth.js25
2 files changed, 45 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
diff --git a/src/helpers/auth.js b/src/helpers/auth.js
new file mode 100644
index 00000000..4504564c
--- /dev/null
+++ b/src/helpers/auth.js
@@ -0,0 +1,25 @@
+import { deleteCookie, getCookie, setCookie } from 'cookies-next';
+
+const getAuth = () => {
+ let auth = getCookie('auth');
+ if (auth) {
+ return JSON.parse(auth);
+ }
+ return false;
+}
+
+const setAuth = (user) => {
+ setCookie('auth', JSON.stringify(user));
+ return true;
+}
+
+const deleteAuth = () => {
+ deleteCookie('auth');
+ return true;
+}
+
+export {
+ getAuth,
+ setAuth,
+ deleteAuth
+}; \ No newline at end of file