From 28fbb1b12964ab710460f64aad6f17e3d7602e47 Mon Sep 17 00:00:00 2001 From: gominzip Date: Sun, 25 Aug 2024 22:46:45 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=ED=8E=98=EC=9D=B4=EC=A7=80=20=EB=B3=84?= =?UTF-8?q?=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EA=B2=80=EC=A6=9D=EC=9D=84=20?= =?UTF-8?q?=EC=9C=84=ED=95=9C=20PrivateRoute=20=EC=BB=B4=ED=8F=AC=EB=84=8C?= =?UTF-8?q?=ED=8A=B8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/authAxois.ts | 1 + src/utils/PrivateRoute.tsx | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 src/utils/PrivateRoute.tsx diff --git a/src/apis/authAxois.ts b/src/apis/authAxois.ts index e6df225..a9d8afc 100644 --- a/src/apis/authAxois.ts +++ b/src/apis/authAxois.ts @@ -13,6 +13,7 @@ export const checkAndRefreshToken = async () => { const refreshToken = LocalStorage.getItem("refresh"); if (!accessToken || !refreshToken) { + alert("로그인 후 이용이 가능합니다!"); return null; } diff --git a/src/utils/PrivateRoute.tsx b/src/utils/PrivateRoute.tsx new file mode 100644 index 0000000..a4cd65b --- /dev/null +++ b/src/utils/PrivateRoute.tsx @@ -0,0 +1,28 @@ +import { AuthVerify } from "@apis/authAxois"; +import { useEffect, useState } from "react"; +import { Navigate } from "react-router-dom"; + +interface PrivateRouteProps { + element: JSX.Element; + [key: string]: any; +} + +const PrivateRoute = ({ element }: PrivateRouteProps) => { + const [isAuthenticated, setIsAuthenticated] = useState(null); + + useEffect(() => { + const checkAuth = async () => { + const result = await AuthVerify(); + setIsAuthenticated(result); + }; + checkAuth(); + }, []); + + if (isAuthenticated === null) { + return; + } + + return isAuthenticated ? element : ; +}; + +export default PrivateRoute;