RequireAuth.tsx 472 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) {
Yoon, Daeki's avatar
Yoon, Daeki committed
10
    alert("로그인이 필요합니다.");
Yoon, Daeki's avatar
Yoon, Daeki committed
11
12
13
14
15
16
    return (
      <Navigate to={"/login"} state={{ from: location.pathname }} replace />
    );
  }
  return children;
};