summaryrefslogtreecommitdiff
path: root/src/components/WithAuth.js
blob: 74518b3b51e32622cd2438cbf7c21b2339d48796 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;