Skip to content

Commit

Permalink
Refactor: user 없으면 signin 페이지로 리다이렉트
Browse files Browse the repository at this point in the history
  • Loading branch information
hongjw030 committed Dec 18, 2023
1 parent d860374 commit 4944acb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/contexts/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,9 @@ export function AuthProvider({ children }: { children: ReactNode }) {
}

// 유저 인증이 필요한 페이지에선 required true 값을 주고,
// required가 false인데 context.user 값이 없고 로딩 중이 아니라면 로그인 페이지로 이동시킨다.
// required가 true인데 context.user 값이 없고 로딩 중이 아니라면 로그인 페이지로 이동시킨다.
export function useAuth(required = false) {
const context = useContext(AuthContext);
console.log(context);
const router = useRouter();
if (!context) {
throw new Error("반드시 AuthProvider 안에서 사용해야 합니다.");
Expand All @@ -108,11 +107,12 @@ export function useAuth(required = false) {
useEffect(() => {
if (
router.route !== "/signup" &&
context.user === null &&
!context.user &&
required &&
!context.isPending
) {
router.push("/signin");
return;
}
}, [context.user, context.isPending, required]);

Expand Down
5 changes: 5 additions & 0 deletions src/pages/folder/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ export default function FolderPage() {
}, []);

//TODO - currentFolder을 의존성에 넣으면 무한렌더링 발생. 왜?? 나중에 SSR로 고치기
const router = useRouter();
useEffect(() => {
if (!user) {
router.push("/signin");
return;
}
getFolderTagList();
getCards(currentFolder);
}, [getFolderTagList, getCards]);
Expand Down

0 comments on commit 4944acb

Please sign in to comment.