-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
48 lines (36 loc) · 1.27 KB
/
middleware.ts
File metadata and controls
48 lines (36 loc) · 1.27 KB
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
44
45
46
47
48
import { type NextRequest, NextResponse } from "next/server";
const publicRoutes = ["/", "/login", "/explore", "/search", "/capsule-detail"];
const protectedRoutes = ["/setting", "/create-capsule", "/my-capsule"];
const isPublicPath = (pathname: string): boolean => {
if (protectedRoutes.some((route) => pathname.startsWith(route))) {
return false;
}
if (publicRoutes.some((route) => pathname.startsWith(route))) {
return true;
}
return false;
};
const getAccessToken = (request: NextRequest): string | undefined => {
return request.cookies.get("accessToken")?.value;
};
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
if (pathname.startsWith("/api/")) {
return NextResponse.next();
}
if (isPublicPath(pathname)) {
return NextResponse.next();
}
// 보호된 경로는 토큰 확인
const accessToken = getAccessToken(request);
if (!accessToken) {
const loginUrl = new URL("/login", request.url);
const originalUrl = request.nextUrl.pathname + request.nextUrl.search;
loginUrl.searchParams.set("next", originalUrl);
return NextResponse.redirect(loginUrl);
}
return NextResponse.next();
}
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};