Skip to content

Commit

Permalink
fix: 새로고침 수정 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
presentKey committed Nov 27, 2023
1 parent 16f9750 commit 97883ca
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/routes/privateRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import { RouteObject, useNavigate } from "react-router-dom";

import Layout from "@/components/Layout";
import useAuth from "@/features/auth/hooks/useAuth";
import useRedirect from "@/hooks/useRedirect";

import RouteLayout from "../components/Layout/RouteLayout";

const Profile = lazy(() => import("@/features/users/routes/Profile"));
const ProfileEdit = lazy(() => import("@/features/users/routes/Edit"));

function PrivateRoute({ children }: PropsWithChildren) {
const { user } = useAuth();
const { user, fetchUser } = useAuth();
const navigate = useNavigate();
const { redirectUrl, handleRedirect } = useRedirect();

/**
* 유저 정보가 없다면 로그인 페이지로 이동합니다.
Expand All @@ -21,10 +23,19 @@ function PrivateRoute({ children }: PropsWithChildren) {
console.log(user);

if (!user) {
if (redirectUrl) {
const handleFetchUser = async () => {
console.log("PrivateRoute redirect handleFetchUser");
await fetchUser();
handleRedirect();
};

handleFetchUser();
}
console.log("PrivateRoute user가 없어서 로그인 페이지로 이동");
navigate("/login", { replace: true });
}
}, [navigate, user]);
}, [navigate, user, redirectUrl]);

return children;
}
Expand Down

0 comments on commit 97883ca

Please sign in to comment.