Skip to content

Commit c734180

Browse files
committed
feat(auth) : auth 접근 시 로그인 상태면 홈으로 가도록, protect 접근 시 비로그인 상태면 홈으로 가도록 구현
1 parent 38ae675 commit c734180

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

src/app/(auth)/layout.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
export default function Layout({ children }: { children: React.ReactNode }) {
1+
import { getMe } from "@/lib/auth/auth.server";
2+
import { redirect } from "next/navigation";
3+
4+
export default async function Layout({ children }: { children: React.ReactNode }) {
5+
let isLoggedIn = false;
6+
7+
try {
8+
await getMe();
9+
isLoggedIn = true;
10+
} catch {}
11+
12+
if (isLoggedIn) {
13+
redirect("/home");
14+
}
15+
216
return (
317
<div className="grid min-h-screen grid-cols-2">
418
{/* TODO: 왼쪽 이미지 */}

src/app/(protect)/layout.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1-
export default function Layout({ children }: { children: React.ReactNode }) {
1+
import { getMe } from "@/lib/auth/auth.server";
2+
import { redirect } from "next/navigation";
3+
4+
export default async function Layout({ children }: { children: React.ReactNode }) {
5+
let isLoggedIn = false;
6+
7+
try {
8+
await getMe();
9+
isLoggedIn = true;
10+
} catch {}
11+
12+
if (!isLoggedIn) {
13+
redirect("/sign-in");
14+
}
15+
216
return <>{children}</>;
317
}

0 commit comments

Comments
 (0)