-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
43 lines (35 loc) · 1.33 KB
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { defaultPath, protectedPaths, publicOnlyPaths } from "./utils/path";
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// 특정 경로는 미들웨어 처리에서 제외
const ignorePaths = ["/api", "/_next", "/static", "/images", "/favicon.ico"];
if (ignorePaths.some((path) => pathname.startsWith(path))) {
return NextResponse.next();
}
// 기본 경로는 home
if (pathname === "/") {
return NextResponse.redirect(new URL(defaultPath, request.url));
}
const isProtectedPath = protectedPaths.some((path) =>
pathname.startsWith(path),
);
const isPublicOnlyPath = publicOnlyPaths.includes(pathname);
const accessToken = request.cookies.get("accessToken");
// 로그인 상태에 따른 리다이렉션
if (accessToken) {
// 로그인한 사용자가 공개 경로 접근 시도
if (isPublicOnlyPath) {
return NextResponse.redirect(new URL(defaultPath, request.url));
}
} else {
// 로그인하지 않은 사용자가 보호된 경로 접근 시도
if (isProtectedPath) {
const url = new URL("/login", request.url);
url.searchParams.set("from", pathname);
return NextResponse.redirect(url);
}
}
return NextResponse.next();
}