RequireAuth.tsx 409 Bytes
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
2
3
4
5
6
7
8
9
import React, { FC } from "react";
import { Navigate, useLocation } from "react-router-dom";
import { useAuth } from "./auth.context";

export const RequireAuth: FC<{ children: JSX.Element }> = ({ children }) => {
  const { user } = useAuth();
  const location = useLocation();

  if (!user.isLoggedIn) {
Kim, MinGyu's avatar
Kim, MinGyu committed
10
    return <Navigate to={"/"} state={{ from: location.pathname }} replace />;
Yoon, Daeki's avatar
Yoon, Daeki committed
11
12
13
  }
  return children;
};