Skip to content

Commit

Permalink
feat: 페이지 별 로그인 검증을 위한 PrivateRoute 컴포넌트 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
gominzip committed Aug 25, 2024
1 parent 9f8ddb1 commit 28fbb1b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/apis/authAxois.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const checkAndRefreshToken = async () => {
const refreshToken = LocalStorage.getItem("refresh");

if (!accessToken || !refreshToken) {
alert("로그인 후 이용이 가능합니다!");
return null;
}

Expand Down
28 changes: 28 additions & 0 deletions src/utils/PrivateRoute.tsx
Original file line number Diff line number Diff line change
@@ -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<boolean | null>(null);

useEffect(() => {
const checkAuth = async () => {
const result = await AuthVerify();
setIsAuthenticated(result);
};
checkAuth();
}, []);

if (isAuthenticated === null) {
return;
}

return isAuthenticated ? element : <Navigate to="/login" />;
};

export default PrivateRoute;

0 comments on commit 28fbb1b

Please sign in to comment.